在c#中从XML读取元素

时间:2017-12-18 19:14:09

标签: c# xml streamreader

我有来自网络服务的回复,我的代码如下所示

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>

我想从上面的XML中读取ID,Name和Address1。

如何实现这一目标?我是c#中的XML新手。

有任何帮助吗?提前谢谢。

3 个答案:

答案 0 :(得分:1)

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

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();
            }
        }
    }
}

答案 1 :(得分:0)

使用Xmldocument。使用root语句。它非常适合..阅读它..或使用XPath。还可以阅读它!

答案 2 :(得分:0)

我将您的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);
            }