我必须从这个XML文件中读取ordertext ("This is an example text"):
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<order id="">
<users>
<user id="123456" nick="nick" done="false" />
</users>
<machines>
<machine id="1234" sd="1234" ref="" done="false" />
</machines>
<todos />
<ordertexts>
<ordertext>This is an example text </ordertext>
</ordertexts>
</order>我的C#代码如下所示:
XmlDocument xDoc = new XmlDocument();
xDoc.Load(file);
XmlElement node = (XmlElement)xDoc.SelectSingleNode("/order/ordertexts/ordertext");当我将所选数据写入另一个XML文件时,它看起来如下所示:
<order>
<oldOrderText>System.Xml.XmlElement</oldOrderText>
</order>我做错什么了?XPath是否不正确?
我是一个C#新手,所以我真的需要我能得到的每一个帮助!
提前谢谢你,基比
发布于 2018-08-28 20:25:45
你要找的是XmlElement.InnerText。
当您使用以下命令获取节点时:
XmlElement node = (XmlElement)xDoc.SelectSingleNode("/order/ordertexts/ordertext");您仍然需要使用以下代码:
string neededText = node.InnerText;来获取该节点的值。
假设您在一个控制台应用程序中编写结果。如果您尝试编写node变量,请使用以下方法:
Console.WriteLine(node);由于node不是字符串,而是一个XmlElement对象,因此将调用XmlElement的ToString方法,该方法返回对象名,因此新的XmlElement的结果为System.Xml.XmlElement,而不是所需的文本。
https://stackoverflow.com/questions/52057633
复制相似问题