Re: Cannot find root element in XML file
- From: TT (Tom Tempelaere) <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
- Date: Wed, 23 Aug 2006 06:21:02 -0700
Hi Martin,
I may have cried Victory a little too early. None of your alternative
suggestions works (I should have tested first). See inline for some comment.
"Martin Honnen" wrote:
TT (Tom Tempelaere) wrote:
<IncidentDefinitions xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tempuri.org/XMLSchema.xsd
C:/IncidentDefinitions.xsd">
....
</IncidentDefinitions>
(3) The code in .NET to query for the root element:
[code]
XmlDocument xmlDoc;
xmlDoc.Load( xmlFileName );
XmlNode node = xmlDoc.SelectSingleNode( "IncidentDefinitions" );
Well if you simply want to access the root element then
xmlDoc.DocumentElement
will do whatever the name of the element is and whatever namespace that
element has (where the namespace in your XML sample is the reason that
your SelectSingleNode does not find the element).
Thus if you need to root element simply use
XmlElement rootElement = xmlDoc.DocumentElement;
Well to be honest I thought this would solve the problem, but it actually
doesn't. If instead of querying the 'IncidentDefinitions' root element I use
xmlDocument.DocumentElement I still have a problem: I can't find any other
nodes that are in the document.
I don't think it makes sense to use XPath and SelectSingleNode to look
for something the object model gives as a property.
As for SelectSingleNode to find an element with XPath in a certain
namespace you would need e.g.
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("pf", "http://tempuri.org/XMLSchema.xsd");
XmlElement rootElement =
xmlDoc.SelectSingleNode("pf:IncidentDefinitions", namespaceManager) as
XmlElement;
Doesn't work either. It does return an element, but querying for any of the
documents nodes fails.
It appears as though the attributes that are added by XmlSpy cannot be
parsed correctly by .NET XML framework, or at least confuses it. Or maybe
there is something else still that I'm not doing correct?
In the meanwhile I'll have to ask my clients to not publish any XML files
with attributes for the root element.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Kind regards,
--
Tom Tempelaere.
.
- Follow-Ups:
- Re: Cannot find root element in XML file
- From: Martin Honnen
- Re: Cannot find root element in XML file
- References:
- Re: Cannot find root element in XML file
- From: Martin Honnen
- Re: Cannot find root element in XML file
- Prev by Date: Re: Text attribute is an array.
- Next by Date: Re: Cannot find root element in XML file
- Previous by thread: Re: Cannot find root element in XML file
- Next by thread: Re: Cannot find root element in XML file
- Index(es):
Relevant Pages
|