C#-XML子节点到文本框

C#-XML子节点到文本框,c#,xml,xpath,C#,Xml,Xpath,我在将子节点文本放入c#中的富文本框时遇到问题。以下是我迄今为止所尝试的: 以下是XML文件: <DATA_LIST> <Customer> <Full_Name>TEST</Full_Name> <Total_Price>100</Total_Price> <Discounts>20</Discounts> </Customer> </DATA_LI

我在将子节点文本放入c#中的富文本框时遇到问题。以下是我迄今为止所尝试的:

以下是XML文件:

<DATA_LIST>
  <Customer>
    <Full_Name>TEST</Full_Name>
    <Total_Price>100</Total_Price>
    <Discounts>20</Discounts>
  </Customer>
</DATA_LIST>
这是我得到的错误:

类型为“System.Xml.XPath.XPathException”的未处理异常 发生在System.Xml.dll中

其他信息:“数据\列表\客户”具有无效令牌


任何帮助都很好。

您选择客户节点的XPath错误,应该是
/DATA\u LIST/Customer


有关更多详细信息和示例,请参阅。

在XPath中不能使用反斜杠。请改用斜杠:

XmlNode xNode = doc.SelectSingleNode("DATA_LIST/Customer");
string TotalPriceString = xNode.SelectSingleNode("Total_Price").InnerText;
您可以使用单个XPath获取价格:

 string totalPrice =  
    doc.SelectSingleNode("DATA_LIST/Customer/Total_Price").InnerText;
另一个建议是使用LINQ到XML。你可以得到价格作为数字

var xdoc = XDocument.Load(path_to_xml);
int totalPrice = (int)xdoc.XPathSelectElement("DATA_LIST/Customer/Total_Price");
或者不使用XPath:

int totalPrice = (int)xdoc.Root.Element("Customer").Element("Total_Price");

多么愚蠢的错误啊。稍微调整一下,现在可以工作了。谢谢
int totalPrice = (int)xdoc.Root.Element("Customer").Element("Total_Price");