无法使用 LINQ to XML 遍历 xml 文档

Unable to use LINQ to XML to loop through xml document

我正在尝试使用 LINQ 获取以下 XML 文档的元素值,但我收到“未设置对象实例的对象引用”。;

XML:

<soapenv:Envelope xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/ 
xmlns:xsd=http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance>

   <soapenv:Body>

  <ns1:GetContactResponse soapenv:encodingStyle=http://schemas.xmlsoap.org/soap/encoding/ xmlns:ns1=http://DefaultNamespace>

     <GetContactReturn xsi:type="ns2:Response" xmlns:ns2=http://getContact.contact.V300>

        <Contact xsi:type="ns3:Contact" xmlns:ns3=http://_contact.contact.V300>

           <Address xsi:type="xsd:string">123 test</Address>

           <Address2 xsi:type="xsd:string"/>

           <City xsi:type="xsd:string">Los Angeles</City>

           <CountryCode xsi:type="xsd:string">US</CountryCode>

           <StateCode xsi:type="xsd:string">CA</StateCode>

           <ZipCode xsi:type="xsd:string">90001</ZipCode>

        </Contact>

        <Errors soapenc:arrayType="ns4:Error[0]" xsi:type="soapenc:Array" xmlns:ns4=http://response.base.V300 xmlns:soapenc=http://schemas.xmlsoap.org/soap/encoding//>

        <IsSuccessful xsi:type="xsd:boolean">true</IsSuccessful>

        <RecordCount xsi:type="xsd:double">1.0</RecordCount>

     </GetContactReturn>

  </ns1:GetContactResponse>

   </soapenv:Body>

 </soapenv:Envelope>

我正在使用以下循环:

using (StreamReader rd = new StreamReader(services.GetResponseStream()))

   {
                        var ServiceResult = rd.ReadToEnd();

                        XDocument xdoc = new XDocument();

                        xdoc = XDocument.Parse(ServiceResult);

                        var xDocResult = (from x in xdoc.Descendants("Contacts")

                                          select new Location

                                          {

                                              Address = x.Element("Address").Value,

                                              Address2 = x.Element("Address2").Value,

                                              city = x.Element("City").Value,

                                              statecode = x.Element("StateCode").Value,

                                              zipcode = x.Element("ZipCode").Value

                                          });
}

我假设这是因为没有根。如何获取各个元素的值?

您的 xml 有很多错误。我正在使用 StringReader。您应该替换为 StreamReader。这里是更正

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <ns1:GetContactResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
      <GetContactReturn xsi:type="ns2:Response" xmlns:ns2="http://getContact.contact.V300">
        <Contact xsi:type="ns3:Contact" xmlns:ns3="http://_contact.contact.V300">
          <Address xsi:type="xsd:string">123 test</Address>
          <Address2 xsi:type="xsd:string"/>
          <City xsi:type="xsd:string">Los Angeles</City>
          <CountryCode xsi:type="xsd:string">US</CountryCode>
          <StateCode xsi:type="xsd:string">CA</StateCode>
          <ZipCode xsi:type="xsd:string">90001</ZipCode>
        </Contact>
        <Errors soapenv:arrayType="ns4:Error[0]" xsi:type="soapenc:Array" xmlns:ns4="http://response.base.V300 xmlns:soapenc=http://schemas.xmlsoap.org/soap/encoding"/>
        <IsSuccessful xsi:type="xsd:boolean">true</IsSuccessful>
        <RecordCount xsi:type="xsd:double">1.0</RecordCount>
      </GetContactReturn>
    </ns1:GetContactResponse>
  </soapenv:Body>
</soapenv:Envelope>

这是 C# 代码

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)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader reader = new StringReader(xml);

            XDocument doc = XDocument.Load(reader);

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

            string address = (string)contact.Element(ns + "Address");
            string address2 = (string)contact.Element(ns + "Address2");
            string city = (string)contact.Element(ns + "City");
            string country = (string)contact.Element(ns + "CountryCode");
            string state = (string)contact.Element(ns + "StateCode");
            string zip = (string)contact.Element(ns + "ZipCode");

        }
    }
}