简体   繁体   English

空引用尝试使用LINQ to XML将XML解析为对象列表时出现异常

[英]Null reference Exception when trying to parse XML to object List using LINQ to XML

I need to create a list of objects from a returned XML using LINQ To XML but every time i try to do that i faced a null reference exception. 我需要使用LINQ To XML从返回的XML创建对象列表,但是每次尝试这样做时,我都会遇到null引用异常。

I trying to making a lot of changes, but i don't know why that error? 我试图进行很多更改,但是我不知道为什么会出错?

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# Code: 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();

Exception: 例外:

Exception Found: 发现异常:
Type: System.NullReferenceException 类型:System.NullReferenceException
Message: Object reference not set to an instance of an object. 消息:对象引用未设置为对象的实例。
Source: QLM.BL 资料来源:QLM.BL
Stacktrace: at BL.LicenseManager. Stacktrace:位于BL.LicenseManager。 b__1(XElement e) in c:.....cs:line 464 c:..... cs:第464行中的b__1(XElement e)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() 在System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 在System.Collections.Generic.List`1..ctor(IEnumerable`1集合)处
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 在System.Linq.Enumerable.ToList [TSource](IEnumerable`1源)处
at QLM.BL.LicenseManager.GetAllActivationKeysByUserId(Int32 id) in c:......cs:line 463 在QLM.BL.LicenseManager.GetAllActivationKeysByUserId(Int32 id)在c:...... cs:第463行

Try to use Convert.ToString.If value is null you will get exception in your .ToString code. 尝试使用Convert.ToString。如果value为null,则将在.ToString代码中获得异常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM