Im new in C# and XML data ussage.
I’ve got the following xml data.
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<response>
<auctions>
<auction>
<id>90436</id>
<user>blabla</user>
<title>title name</title>
<value>10000.00</value>
<period>36</period>
<www/>
</auction>
<auction>
<id>90436</id>
<user>blabla</user>
<title>title name</title>
<value>10000.00</value>
<period>36</period>
<www/>
</auction>
</auctions>
</response>
I use that C# code. (it’s class used by Form1)
public IXmlNamespaceResolver ns { get; set; }
public string[] user,id,title,value,period;
public void XmlRead(string url)
{
// Create a new XmlDocument
XPathDocument doc = new XPathDocument(url);
// Create navigator
XPathNavigator navigator = doc.CreateNavigator();
// Get forecast with XPath
XPathNodeIterator nodes = navigator.Select("/response/auctions", ns);
int i = 0;
foreach (XPathNavigator oCurrentPerson in nodes)
{
userName[i] = oCurrentPerson.SelectSingleNode("user").Value;
userId[i] = int.Parse(oCurrentPerson.SelectSingleNode("id").Value);
title[i] = oCurrentPerson.SelectSingleNode("title").Value;
value[i] = oCurrentPerson.SelectSingleNode("value").Value;
period[i] = oCurrentPerson.SelectSingleNode("period").Value;
i++; }
}
Im getting an error:
Object reference not set to an instance of an object.
userName[i] = oCurrentPerson.SelectSingleNode(“user”).Value;
When I used single string variables like: userName, userId without [] everything worked correctly.
Thanks in advance
.Net is a strongly-typed world, so use it’s benefits. Create
Auctionclass to hold data from your xml:And parse your xml with Linq to Xml: