在 C# 中读取 XML 中的元素

Read Elements from XML in c#

我收到来自网络服务的响应,我的代码如下所示

using (WebResponse response2 = request.GetResponse())
{
    using (StreamReader rd = new StreamReader(response2.GetResponseStream()))
    {
       string soapResult = rd.ReadToEnd();                   
    }
}

现在我对字符串 soapResult 有了完整的响应。

我的 XML 如下所示:

soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
  <GetResponse xmlns="http://ws.design.americaneagle.com">
     <GetResult>
        <Distance>0</Distance>
        <ID>100</ID>
        <Name>Wisconsin</Name>
        <Code>WI</Code>
        <Address1>202 Las COlinas</Address1>
    </GetResult>
  </GetResponse>
 </soap:Body>
 </soap:Envelope>

我想从上面读取 ID、Name 和 Address1 XML。

如何实现?我是 c# 中 XML 的新手。

有什么帮助吗?提前致谢。

使用 xml linq。为了进行测试,我正在从文件(而不是网络响应)中读取,因此您必须稍作更改才能返回到原始代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            using (StreamReader rd = new StreamReader(FILENAME))
            {
                XDocument doc = XDocument.Load(rd);

                XElement response = doc.Descendants().Where(x => x.Name.LocalName == "GetResponse").FirstOrDefault();
                XNamespace ns = response.GetDefaultNamespace();

                var data = response.Descendants(ns + "GetResult").Select(x => new {
                    distance = (int)x.Element(ns + "Distance"),
                    id = (int)x.Element(ns + "ID"),
                    name = (string)x.Element(ns + "Name"),
                    code = (string)x.Element(ns + "Code"),
                    addresss = (string)x.Element(ns + "Address1")
                }).FirstOrDefault();
            }
        }
    }
}

使用 Xmldocument。用 use root 语句。它非常好用……阅读它……或使用 XPath。另请阅读!

我将你的 XML 粘贴到 XML to Sharp 并得到了这个:

            using System;
            using System.Xml.Serialization;
            using System.Collections.Generic;

            namespace Xml2CSharp
            {
                [XmlRoot(ElementName = "GetResult", Namespace = "http://ws.design.americaneagle.com")]
                public class GetResult
                {
                    [XmlElement(ElementName = "Distance", Namespace = "http://ws.design.americaneagle.com")]
                    public string Distance { get; set; }

                    [XmlElement(ElementName = "ID", Namespace = "http://ws.design.americaneagle.com")]
                    public string ID { get; set; }

                    [XmlElement(ElementName = "Name", Namespace = "http://ws.design.americaneagle.com")]
                    public string Name { get; set; }

                    [XmlElement(ElementName = "Code", Namespace = "http://ws.design.americaneagle.com")]
                    public string Code { get; set; }

                    [XmlElement(ElementName = "Address1", Namespace = "http://ws.design.americaneagle.com")]
                    public string Address1 { get; set; }
                }

                [XmlRoot(ElementName = "GetResponse", Namespace = "http://ws.design.americaneagle.com")]
                public class GetResponse
                {
                    [XmlElement(ElementName = "GetResult", Namespace = "http://ws.design.americaneagle.com")]
                    public GetResult GetResult { get; set; }

                    [XmlAttribute(AttributeName = "xmlns")]
                    public string Xmlns { get; set; }
                }

                [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
                public class Body
                {
                    [XmlElement(ElementName = "GetResponse", Namespace = "http://ws.design.americaneagle.com")]
                    public GetResponse GetResponse { get; set; }
                }

                [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
                public class Envelope
                {
                    [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
                    public Body Body { get; set; }

                    [XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
                    public string Soap { get; set; }

                    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
                    public string Xsi { get; set; }

                    [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
                    public string Xsd { get; set; }
                }
            }

那你就要反序列化了。

            var serializer = new XmlSerializer(typeof(Envelope));
            Envelope result;

            using (TextReader reader = new StringReader(xml))
            {
                result = (Envelope)serializer.Deserialize(reader);
            }