- Home /
Reading XML Data C#
Hey guys, I'm currently having trouble on trying to read the data from my XML doc. I am fairly new to C# and it's causing some issues for me. I've been looking around and I just can't seem to figure out what I'm doing wrong or whats the right way of doing it since I've found like 100 different ways!
Here's the XML I am trying to read.
<KenoNumbers>
<numbers num="1" />
<numbers num="4" />
<numbers num="32" />
<numbers num="54" />
</KenoNumbers>
Here's where I'm trying to read the XML.
XmlDocument newXml = new XmlDocument ();
newXml.Load(Application.dataPath + "/Script/keno.xml");
XmlNode root = newXml.DocumentElement;
nodeList = root.SelectNodes ("(KenoNumbers/numbers)[1]");
print(nodeList);
If someone could help lead me in the right direction, or let me know if I'm close. Currently nodeList is just showing (Object) in debug.
Answer by spiceboy9994 · May 21, 2015 at 02:01 PM
The reason for that it is because the result for that operation is a XMLNodeList. Check this out
https://msdn.microsoft.com/en-us/library/hcebdtae(v=vs.110).aspx
If you want to get the value of the nodes of the returned list you could try
foreach (XmlNode xn in nodeList)
{
string value1 = xn["MyFirstValue"].InnerText;
string value2 = xn["MySecondValue"].InnerText;
print("Node Values: {0} {1}", value1 , value2 );
}
BTW, I think your XPath for selecting the nodes is not correct. Get of rid of the parenthesis within the path. Here's a link for XPath syntax
http://www.w3schools.com/xpath/xpath_syntax.asp
Regards
I just changed up how I'm grabbing the nodes, but in my print statement it keeps saying object. This is what I just tried... XmlDocument newXml = new XmlDocument (); newXml.Load(Application.dataPath + "/Script/keno.xml");
XmlDocument xmlDoc = new XmlDocument();
numList = xmlDoc.GetElementsByTagName("$$anonymous$$enoNumbers");
print(numList);
@Xzapo, you just ended up with a NodeList that should have for elements in it, each node would be called numbers, each numbers element would have an attribute called num with a string value of whatever the heck is in it.
If you use what you've already done, you need to iterate over the collection in the NodeList object and grab your values. @spiceboy9994 already lined the $$anonymous$$SDN article that shows as much.
That method also returns a nodelist
https://msdn.microsoft.com/en-us/library/dc0c9ekk(v=vs.110).aspx
Just add the foreach as I mentioned before... and replace the strings for your node inner nodes. Or if you do not have inner nodes and just want to print the first node value, try this:
var firstNode = numList[0];
print(firstNode.Value);
Answer by vks123 · Sep 11, 2020 at 09:18 AM
It is an old answer, but I will suggest to use Linq when parsng XML, here is an example, Where books are fetched
var NodeStr = "";
//get all elements inside book
foreach (XElement level1Element in XElement.Load(mainpath).Elements("book"))
{
//print each element value
//you can also print XML attribute value, instead of .Element use .Attribute
NodeStr = NodeStr + "\nAuthor " + level1Element.Element("author").Value;
NodeStr = NodeStr + "\nTitle " + level1Element.Element("title").Value;
NodeStr = NodeStr + "\nGenre " + level1Element.Element("genre").Value;
NodeStr = NodeStr + "\nPrice " + level1Element.Element("price").Value;
NodeStr = NodeStr + "\nDescription -" + level1Element.Element("description").Value;
}
Source: Open and Read XML in C# (Examples using Linq, XMLReader, XMLDocument)
Your answer
Follow this Question
Related Questions
Xml Serialization? (how to read/write?) 1 Answer
Unity C# Post XML to endpoint 0 Answers
XML Data binding in unity 5 using c# 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers