空引用尝试使用LINQ to XML将XML解析为对象List时的异常

时间:2015-07-16 10:51:06

标签: c# .net xml linq

我需要使用LINQ To XML从返回的XML创建一个对象列表,但每次我尝试这样做时都会遇到空引用异常。

我试图做出很多改变,但我不知道为什么会出现这种错误?

XML:

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" minOccurs="0" />
                <xs:element name="ActivationKey" type="xs:string" minOccurs="0" />

                <xs:element name="FloatingSeats" type="xs:int" minOccurs="0" />
                <xs:element name="FloatingLicenseLocation" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table>
    <ID>3</ID>
    <ActivationKey>dummy111</ActivationKey>
    <ComputerKey>UUGH080S01CBCN8882HR336H5M</ComputerKey>
    <ComputerID>NWADIE-8-6015</ComputerID>
    <UserID>0</UserID>
    <ProductID>3</ProductID>
    <MajorVersion>6</MajorVersion>
    <MinorVersion>1</MinorVersion>
    <OrderDate>2015-05-14T09:11:18</OrderDate>
    <ActivationDate>2015-05-26T11:41:50</ActivationDate>
    <LastAccessedDate>2015-05-26T11:41:50</LastAccessedDate>
    <CreationDate>2015-05-14T09:11:18</CreationDate>
    <ActivationCount>3</ActivationCount>
    <OrderID />
    <Comment />
    <GenericLicense>false</GenericLicense>
    <ReleaseCount>2</ReleaseCount>
    <ReleaseDate>2015-05-25T12:07:48</ReleaseDate>
    <NumLicenses>1</NumLicenses>
    <AvailableLicenses>0</AvailableLicenses>
    <ComputerName>NWADIE-8-6015</ComputerName>
    <Disabled>false</Disabled>
    <UserData1 />
    <AffiliateID>None</AffiliateID>
    <ReceiptID />
    <OrderStatus>8</OrderStatus>
    <FloatingSeats>0</FloatingSeats>
  </Table>
  <Table>
    <ID>4</ID>
    <ActivationKey>Dummy222</ActivationKey>
    <ComputerKey>UDGR0H0Q01EJ6MCE87HW3G6DFZ</ComputerKey>
    <ComputerID>YKAMAL-8-4631</ComputerID>
    <UserID>0</UserID>
    <ProductID>3</ProductID>
    <MajorVersion>6</MajorVersion>
    <MinorVersion>1</MinorVersion>
    <OrderDate>2015-05-14T09:11:18</OrderDate>
    <ActivationDate>2015-07-13T10:59:09</ActivationDate>
    <LastAccessedDate>2015-07-13T10:59:09</LastAccessedDate>
    <CreationDate>2015-05-14T09:11:18</CreationDate>
    <ActivationCount>16</ActivationCount>
    <OrderID />
    <Comment />
    <GenericLicense>false</GenericLicense>
    <ReleaseCount>15</ReleaseCount>
    <ReleaseDate>2015-07-13T10:50:35</ReleaseDate>
    <NumLicenses>1</NumLicenses>
    <AvailableLicenses>0</AvailableLicenses>
    <ComputerName>YKAMAL-8-4631</ComputerName>
    <Disabled>false</Disabled>
    <UserData1 />
    <AffiliateID>None</AffiliateID>
    <ReceiptID />
    <OrderStatus>8</OrderStatus>
    <FloatingSeats>0</FloatingSeats>
  </Table>
</NewDataSet>

C#代码:

XDocument xDocLinq = XDocument.Parse(xmlFormattedString);
string neededChild = "Table";

// Fatel error in parsing here.
var qlmLicenseByUserId = (from e in xDocLinq.Root.Elements(neededChild)
    select new LocalQlmLicense
    {
        ID = Convert.ToInt32(e.Element("ID").Value),
            ActivationKey = e.Element("ActivationKey").Value.ToString(),
            ComputerKey = e.Element("ComputerKey").Value.ToString(),
            UserID = Convert.ToInt32(e.Element("UserID").Value),
            ProductID = Convert.ToInt32(e.Element("ProductID").Value),
            NumLicenses = Convert.ToInt32(e.Element("NumLicenses").Value),
            AvailableLicenses = Convert.ToInt32(e.Element("AvailableLicenses").Value)
        }).ToList();

例外:

  

发现例外:
  键入:System.NullReferenceException
  消息:对象引用未设置为对象的实例   资料来源:QLM.BL
  Stacktrace:在BL.LicenseManager。 b__1(XElement e)c:..... cs:第464行      在System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     在System.Collections.Generic.List`1..ctor(IEnumerable`1集合)
     在System.Linq.Enumerable.ToList [TSource](IEnumerable`1 source)
     在QLM.L.RicenseManager.GetAllActivationKeysByUserId(Int32 id)in c:...... cs:line 463

1 个答案:

答案 0 :(得分:0)

尝试使用Convert.ToString.If value为null,您将在.ToString代码中获得异常。

   ActivationKey = Convert.ToString e.Element("ActivationKey").Value),
   ComputerKey =  Convert.ToStringe.Element("ComputerKey").Value),