现在,我想获取 XML 文件中的一些特定信息。这是我对 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>
我在网上查看了如何使用代码执行此操作,并且看到了这个实现。然后我将其改编为我的代码:
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);
}
到目前为止,一切正常。 这里的问题是当我在 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>
然后,它只是打开一个空的控制台窗口:
这里的问题到底是什么?我假设它会检查第一个区域是否具有名称 TestRegion,但由于它没有名称,因此它会失败并停止检查其余区域。
最佳答案
您需要做的是在 linq-to-xml 中链接您的查询,一次选择每组元素。
// all worlds
var worlds = xelement.Elements("world");
// all regions in the worlds where the attribute "name" is "TestRegion"
var regions = worlds.Elements("region")
.Where (region => (string)region.Attribute("name") == "TestRegion");
// all areas in the specified regions where the attribute "name" is "TestArea"
var areas = regions.Elements("area")
.Where (area => (string)area.Attribute("name") == "TestArea");
// all buildings in the specified areas where the attribute "name" is "Outside"
var buildings = areas.Elements("building")
.Where (building => (string)building.Attribute("name") == "Outside");
// all rooms in the specified buildings where the attribute "name" is "TutorialRoom"
var rooms = buildings.Elements("room")
.Where (room => (string)room.Attribute("name") == "TutorialRoom");
// all stuff to take in the specified rooms
var stuffs = rooms.Elements("stuffToTake");
// all the contents of the stuffs as an enumerable (there might be more than one, after all)
var values = stuffs.Select (stuff => stuff.Value);
foreach (var value in values)
{
Console.WriteLine(value);
}
并且,没有中间变量:
var values = xelement.Elements("world")
.Elements("region").Where (region => (string)region.Attribute("name") == "TestRegion")
.Elements("area").Where (area => (string)area.Attribute("name") == "TestArea")
.Elements("building").Where (building => (string)building.Attribute("name") == "Outside")
.Elements("room").Where (room => (string)room.Attribute("name") == "TutorialRoom")
.Elements("stuffToTake")
.Select(stuff => stuff.Value);
关于c# - C#读取特定的XML数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28496654/