在c#中从XML读取元素

问题描述 投票:-3回答:3

我有来自Web服务的响应,我的代码如下所示

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的新手。

有帮助吗?提前致谢。

c# xml streamreader
3个回答
1
投票

使用xml linq。为了测试我正在读取文件(而不是webresponse),因此您必须稍作修改才能返回原始代码

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

0
投票

使用Xmldocument。使用root语句。这是非常使用..阅读它..或使用XPath。还读一下!


0
投票

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

            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);
            }
© www.soinside.com 2019 - 2024. All rights reserved.