繁体   English   中英

从xml文件中获取元素

[英]Get element from xml file

我有这个问题:

我有一个方法

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

我的输入xml字符串是:

<?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>

我需要从这个字符串XmlElement返回,它看起来像:

<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>

多数民众赞成没有<?xml version ="1.0"?>但是doc.DocumentElements只返回我:

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

有什么办法可以实现吗?

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

doc.DocumentElement.OuterXml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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