Re: Cannot find root element in XML file

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.

.



Relevant Pages

  • newbie: writing to xml and namespaces
    ... I'm having some issues when programmatically editing an XML file. ... XmlTextWriter and add the default namespace declaration when writing the ... appears other than in the root element. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Openxml() Edge Table - root element problem
    ... parameter in sp_xml_preparedocument (instead of using one of the other root element attributes)? ... http://www.ups.com/XMLSchema/EBR/Billing/v1 is the namespace in the default namespace declaration of the root element of your posted XML sample. ... Or should I be searching for the applicable string in the original .xml doc for each new doc? ... or is the openxml() parse strictly internal? ...
    (microsoft.public.sqlserver.xml)
  • Re: Openxml() Edge Table - root element problem
    ... Based on them I did some reading on xml ... element and its descendants elements are in the namespace and for XPath ... prefix to the namespace and use that prefix. ... attribute in the root element, but looks like any name that fits the required ...
    (microsoft.public.sqlserver.xml)
  • Re: Namespace Issue with InnerXML
    ... I'm sure it's probably not a complex XSLT that can remove the root element of an XML file but as I never used XMLT.... ... But your original change is more than only removing the root element, you want to change the namespace of the descendant nodes. ...
    (microsoft.public.dotnet.xml)
  • Re: master / detail datagrid...
    ... Macromedia uses their prefix in the exact same way an XML ... A patent on the other hand can be obtained but unlike the paltry ... and an XML Namespace is the equivalent of a short musical ...
    (microsoft.public.dotnet.framework.aspnet)