Right now, I want to obtain some specific information inside an XML file. Here is what I have for the XML:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<world>
<region name="TestRegion">
<area name="TestArea">
<building name="Outside">
<room name="TutorialRoom">
<stuffToTake>All the text I want to take</stuffToTake>
</room>
</building>
</area>
</region>
</world>
</root>
I looked online on how to do this with code, and I saw this implementation. I just then adapted it to my code:
XElement xelement = XElement.Load("..\\..\\LocationDatabase.xml");
var TextToDisplay = from regions in xelement.Elements("world")
where (string)regions.Element("region").Attribute("name") == "TestRegion"
where (string)regions.Element("region").Element("area").Attribute("name") == "TestArea"
where (string)regions.Element("region").Element("area").Element("building").Attribute("name") == "Outside"
where (string)regions.Element("region").Element("area").Element("building").Element("room").Attribute("name") == "TutorialRoom"
select regions;
foreach (var xEle in TextToDisplay)
{
Console.WriteLine(xEle.Element("region").Element("area").Element("building").Element("room").Element("stuffToTake").Value);
}
And so far, this works fine.  The issue here is when I add some data ABOVE the XML
<?xml version="1.0" encoding="utf-8" ?>
<root>
<world>
<region name="LolNope">
<area name="TestArea">
<building name="Outside">
<room name="TutorialRoom">
<stuffToTake>All the text I want to take</stuffToTake>
</room>
</building>
</area>
</region>
<region name="TestRegion">
<area name="TestArea">
<building name="Outside">
<room name="TutorialRoom">
<stuffToTake>All the text I want to take</stuffToTake>
</room>
</building>
</area>
</region>
</world>
</root>
Then, it just turns an empty Console Window:  
What exactly is the issue here? I am assuming that it checks if the first region has the name TestRegion, but since it doesn't it fails and stops checking the rest.
No comments:
Post a Comment