c# - C#读取特定的XML数据

标签 c# xml

现在,我想获取 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);
}

到目前为止,一切正常。 Working Console window 这里的问题是当我在 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>

然后,它只是打开一个空的控制台窗口: Blank, empty Console Window

这里的问题到底是什么?我假设它会检查第一个区域是否具有名称 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/

相关文章:

c# - 记录 WCF 接口(interface)的最佳方式?

c# - 如何获取 NHibernate 中的预计属性?

c# - .NET Core 控制台应用程序 : The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot

python - 如何在python中检查xml标签值是否为none

c# - 将具有不同元素的 XML 反序列化为单独的列表

c# - 智能 HTML 编码

xml - 如何使 "use"属性(必需/可选)依赖于另一个值?

c# - 检测点击标签,在 ViewCell 内,在 ListView 内

c# - 无法解析 Fabric :/addresses using ServicePartitionResolver. 问题:根据验证过程,远程证书无效

c# - 在任务中包装 Start() 方法和 Completed 事件