简体   繁体   中英

Can't load files that has xmlns attribute in the first child with XDocument

I am new to XDocument but I have been looking around for a solution to this problem which I couldn't get to fix.

I need to load some kind of XML files (PNML) that comes this way:

<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
 <net id="id" type ="http://www.pnml.org/version-2009/grammar/ptnet">
  ..........</net> </pnml>

And I couldn't get to load these kind of files unless I add "xmlns" as an Attribute to the node net . Meanwhile, the files I create myself has this xmlns attribute, and I can load them without problems. While, files that are generated from some other software that I need to be able to use from my software doesn't has this "xmlns" attribute, and if I add it myself to the files generated by this software, I can load those files.

Here's the code I am using to Load :

            XDocument doc = XDocument.Load(file);
            XNamespace ns = @"http://www.pnml.org/version-2009/grammar/pnml";

            foreach (XElement element in doc.Element(ns + "pnml")
                                .Elements("net").Elements("page").Elements("place"))
            { // Do my loading to "place" nodes for example }

But whenever I try to load a file, it just skips my "foreach" statement, and if I add some line before "foreach" like:

string id= (string) doc.Element(ns + "pnml")
                                .Element("net").Attribute("id");

it says:

Object reference not set to an instance of an object.

Here's an example of a file generated by my code and also can be read from my code:

<?xml version="1.0" encoding="utf-8"?>
<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
  <net id="netid" type="http://www.pnml.org/version-2009/grammar/ptnet" xmlns="">
    nodes and information </net> </pnml>

NOTE: I use this code to save my files:

            XNamespace ns = @"http://www.pnml.org/version-2009/grammar/pnml";
            XDocument doc = new XDocument
            (
              new XElement(ns+"pnml"
                , new XElement("net",new XAttribute("id", net_id), ...));

I found a way to save my files without this "xmlns" attribute, but once I omit it, I can't load it from my code. And the first example I wrote is the standard format and I really need to get ride of the "xmlns" problem.

EDIT: I'm sorry if you got confused, what I want is to be able to load the standard PNML files that doesn't have thise "xmlns" attribute within the "net" node.

What you're missing is that element namespaces are inherited from their parents .

So your XML:

<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
    <net id="id" type ="http://www.pnml.org/version-2009/grammar/ptnet">
...

Contains two elements. One is pnml with the namespace http://www.pnml.org/version-2009/grammar/pnml , and the child is net which also has the namespace http://www.pnml.org/version-2009/grammar/pnml .

With this in mind, your query on the existing XML should be:

doc.Element(ns + "pnml").Elements(ns + "net")...

And your code to generate the XML should be:

new XElement(ns + "pnml",
    new XElement(ns + "net", new XAttribute("id", net_id), ...));

试试这个

var result = doc.Element(ns + "pnml").Descendants().Where(x=>x.Name.LocalName=="net")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM