假设我有以下 XML 文档,如何获取 a:name 的元素值(在我的示例中,该值为 Saturday 100)?我的困惑是如何处理 namespace 。谢谢。
我正在使用 C# 和 VSTS 2008。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<PollResponse xmlns="http://tempuri.org/">
<PollResult xmlns:a="http://schemas.datacontract.org/2004/07/FOO.WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:name>Saturday 100</a:name>
</PollResult>
</PollResponse>
</s:Body>
</s:Envelope>
最佳答案
使用 System.Xml.XmlTextReader 类,
System.Xml.XmlTextReader xr = new XmlTextReader(@"file.xml");
while (xr.Read())
{
if (xr.LocalName == "name" && xr.Prefix == "a")
{
xr.Read();
Console.WriteLine(xr.Value);
}
}
关于c# - 使用 C# 获取特定 XML 元素值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1216669/