简体   繁体   中英

Get element from xml file

I have this problem:

I have a method

private  XmlElement ToXmlElement(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return doc.DocumentElement;
        }

And my input xml string is:

<?xml version="1.0"?>
<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:IEEE-1671:2009.02:Common">
<Item name="edsw">
<Collection />
</Item>
</Collection>

I need to return from this string XmlElement, which looks like:

<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:IEEE-1671:2009.02:Common">
    <Item name="edsw">
    <Collection />
    </Item>
    </Collection>

thats everything without <?xml version ="1.0"?> but doc.DocumentElements returns me only:

<Item name="edsw" xmlns="urn:IEEE-1671:2009.02:Common">
<Collection />
</Item>

is there any way how to achieve it?

看看OuterXml ,你可以在那里找到你需要的东西:

doc.DocumentElement.OuterXml

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