I try to return some nested values in XML file.And I want to return for every desk, the value of postal code, weight and Value.
So I have this xml file:
<?xml version="1.0"?>
<Container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Id>684</Id>
<dateCool>2011-08-22T00:00:00+05:00</dateCool>
<desks>
<desk>
<Sender>
<Name>Jan</Name>
<Address>
<Street>bood</Street>
<HouseNumber>22</HouseNumber>
<PostalCode>2402AE</PostalCode>
<City>Al Rijn</City>
</Address>
</Sender>
<participant>
<Name>Piet</Name>
<Address>
<Street>Schan</Street>
<HouseNumber>22</HouseNumber>
<PostalCode>2497AV</PostalCode>
<City>Den Haag</City>
</Address>
</participant>
<Weight>0.02</Weight>
<Value>0.0</Value>
</desk>
<desk>
<Sender xsi:type="Company">
<Name>ykken groot B.V.</Name>
<Address>
<Street>ILSY-PLantsoen</Street>
<HouseNumber>1</HouseNumber>
<PostalCode>2497GA</PostalCode>
<City>Den Haag</City>
</Address>
<CcNumber>65465424</CcNumber>
</Sender>
<participant>
<Name>Aad</Name>
<Address>
<Street>Korenbloemkamp</Street>
<HouseNumber>22</HouseNumber>
<PostalCode>2353HS</PostalCode>
<City>Leiderdorp</City>
</Address>
</participant>
<Weight>2.0</Weight>
<Value>0.0</Value>
</desk>
<desk>
<Sender xsi:type="Company">
<Name>seti</Name>
<Address>
<Street>ILSY-PLantsoen</Street>
<HouseNumber>1</HouseNumber>
<PostalCode>2497GA</PostalCode>
<City>Den Haag</City>
</Address>
<CcNumber>65465424</CcNumber>
</Sender>
<participant>
<Name>Martijn</Name>
<Address>
<Street>Burgemeester Roosstraat</Street>
<HouseNumber>33</HouseNumber>
<PostalCode>3035 AC</PostalCode>
<City>Rotterdam</City>
</Address>
</participant>
<Weight>100.0</Weight>
<Value>2000.0</Value>
</desk>
<desk>
<Sender xsi:type="Company">
<Name>Aad</Name>
<Address>
<Street>Korenbloemkamp</Street>
<HouseNumber>22</HouseNumber>
<PostalCode>2353HS</PostalCode>
<City>Leiderdorp</City>
</Address>
</Sender>
<participant>
<Name>Martijn</Name>
<Address>
<Street>Burge Roosaat</Street>
<HouseNumber>33</HouseNumber>
<PostalCode>3035 AC</PostalCode>
<City>Rotterdam</City>
</Address>
</participant>
<Weight>11</Weight>
<Value>500</Value>
</desk>
</desks>
</Container>
And my code looks like this:
static void Main(string[] args)
{
FileStream fs = new FileStream("Container.xml", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string s = sr.ReadToEnd();
sr.Close();
fs.Close();
List<Dictionary<string, string>> orderList = new List<Dictionary<string, string>>();
XDocument xDocument = XDocument.Parse(s);
//Get all nodes
List<XNode> xNodes = xDocument.DescendantNodes().ToList();
foreach (XNode node in xNodes)
{
XElement element = node as XElement;
if (element.Name != "desk") continue;
Dictionary<string, string> dict = new Dictionary<string, string>();
//For each orderProperty, get all attributes
foreach (XAttribute attribute in element.Attributes())
{
dict.Add(attribute.Name.ToString(), attribute.Value);
}
orderList.Add(dict);
}
foreach (Dictionary<string, string> dict in orderList)
{
foreach (string key in dict.Keys)
{
Console.Write(key + ": " + dict[key] + ", ");
}
Console.Write("\n");
}
}
But I get every time this error:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Its because of some of xnodes are null
Use this code
It works :)
static void Main(string[] args)
{
var s = string.Empty;
using (var fs = new FileStream("Container.xml", FileMode.Open))
{
using (var sr = new StreamReader(fs))
{
s = sr.ReadToEnd();
}
}
var orderList = new List<Dictionary<string, string>>();
var xDocument = XDocument.Parse(s);
var xNodes = xDocument.DescendantNodes().ToList();
foreach (var node in xNodes)
{
var element = node as XElement;
if (element == null) continue;
if (element.Name != "desk") continue;
var dict = new Dictionary<string, string>();
foreach (var deskNode in element.Nodes())
{
var deskElement = deskNode as XElement;
if (deskElement == null) continue;
dict.Add(deskElement.Name.ToString(), deskElement.Value);
}
orderList.Add(dict);
}
foreach (var dict in orderList)
{
foreach (string key in dict.Keys)
{
Console.Write(key + ": " + dict[key] + ", ");
}
Console.Write("\n");
}
Console.ReadKey();
}
And i changed your second foreach loop element.Attributes()
return null