当尝试使用LINQ to XML解析XML到对象列表时,Null引用异常

本文关键字:XML 列表 对象 异常 引用 Null 解析 to LINQ | 更新日期: 2023-09-27 18:03:12

我需要使用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();
异常:

异常发现:
类型:系统。得到NullReferenceException
消息:对象引用未设置为对象的实例。
来源:QLM。提单
Stacktrace:在bl . licensemmanager。b__1(XElement e) in c:.....cs:line 464
2. System.Linq.Enumerable.WhereSelectEnumerableIterator"movenext ()
在System.Collections.Generic.List"1 . .男星(IEnumerable 1集)
在System.Linq.Enumerable。ToList [TSource] (IEnumerable ' 1源)
在QLM.BL.LicenseManager。GetAllActivationKeysByUserId(Int32 id)在c:......cs:第463行

当尝试使用LINQ to XML解析XML到对象列表时,Null引用异常

尝试使用convert . tostring。如果value为null,您将在您的。tostring代码中得到异常。

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