我需要读取 xml 标记中特定值的值,在我尝试过的解决方案中我只能找到要获取标记元素的值,我需要从根元素遍历到子元素。是有一个选项,我可以直接选择一个标签并获取它的值。
在下面的 xml 示例中,我需要使用 C# 从 xml 中获取 123456 值。 例如:-
<ForwardActionRequest xmlns:auth="test" xmlns="http://www.test">
<auth:Authentication>
<auth:AuthenticationData>
<auth:AuthenticationKey>test</auth:AuthenticationKey>
<auth:Username>test</auth:Username>
<auth:Password>test</auth:Password>
</auth:AuthenticationData>
</auth:Authentication>
<SearchOrderReference>
<Reference>123456</Reference>
<AllocatedBy>test</AllocatedBy>
<Description>test</Description>
</SearchOrderReference>
请您参考如下方法:
您可以使用 LINQ to XML:
XDocument doc = XDocument.Load(yourXMLText);
string value = doc.Element("SearchOrderReference").Element("Reference").Value;
请注意,我尚未测试此代码。
我也鼓励您阅读更多关于 LINQ to XML here 的信息.
