Reputation: 2714
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.
I trying to making a lot of changes, but i don't know why that error?
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:
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
Message: Object reference not set to an instance of an object.
Source: QLM.BL
Stacktrace: at BL.LicenseManager. b__1(XElement e) in c:.....cs:line 464
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at QLM.BL.LicenseManager.GetAllActivationKeysByUserId(Int32 id) in c:......cs:line 463
Upvotes: 0
Views: 767
Reputation: 3034
Try to use Convert.ToString.If value is null you will get exception in your .ToString code.
ActivationKey = Convert.ToString e.Element("ActivationKey").Value),
ComputerKey = Convert.ToStringe.Element("ComputerKey").Value),
Upvotes: 0