我有這樣的問題:從XML文件中獲取元素
我有一個方法
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>
有什麼辦法如何實現的呢?
這不會發生在我身上。 'DocumentElement'返回整個'Collection'。你如何將結果轉換爲字符串? – svick
當我調試它時,我看到DocumentElement內部xml只有這個,不是整個集合, –
字符串ToStringElement(XmlElement xml){StringWriter sw = new StringWriter(); XmlTextWriter writter = new XmlTextWritter(sw); xml.WriteTo(writter); return sw.toString(); } –