如何使用XElement在xml中查找其中一个根元素的值?当根元素没有子元素时?

时间:2012-01-10 19:44:02

标签: xml linq xelement

我想使用errorCode找到XElement节点的值。请指教。

<registration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <errorCode>201498</errorCode>
  <errorMessage>XML response de-serialization error.  Details: XML ??(1, 569)?????</errorMessage>
</registration>

1 个答案:

答案 0 :(得分:0)

您只需要将xml加载到XElement中并调用Element方法来检索值

//Load from file location
var errFile = XElement.Load("C:\\YourFile.xml");

//Get Element value from xml
var errorCode = (string)_x.Element("errorCode");
相关问题