使用C#获取特定XML元素值的问题

Geo*_*ge2 2 .net c# xml visual-studio-2008

假设我有以下XML文档,如何获取:name的元素值(在我的示例中,值是星期六100)?我的困惑是如何处理名称空间.谢谢.

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

ada*_*ost 5

使用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);
            }
        }
Run Code Online (Sandbox Code Playgroud)