C# Foreach XML 节点 [英] C# Foreach XML Node

查看:23
本文介绍了C# Foreach XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将二维坐标保存在一个 XML 文件中,其结构类似于:

I'm saving 2-dimensional coordinates on an XML file with a structure similar to:

<?xml version="1.0" encoding="utf-8" ?> 
<grid>
<coordinate time="78">
<initial>540:672</initial> 
<final>540:672</final> 
</coordinate>
</grid>

我可以打开 XML 文件并通过 XmlTextReader 读取它,但我如何专门循环坐标以检索初始节点和最终节点之间的时间属性和数据,格式类似于:

I can open the XML file and read it via the XmlTextReader, but how do I loop through the coordinates specifically to retrieve both the time attribute and data between the initial and final nodes in some format similar to:

string initial = "540:672";
string final  = "540:672";
int time = 78;

<小时>

新代码:

我的新代码:

//Read the XML file.
XDocument xmlDoc = XDocument.Load("C:\test.xml");

foreach (var coordinate in xmlDoc.Descendants("coordinate"))
{
    this.coordinates[this.counter][0] = coordinate.Attribute("time").Value;
    this.coordinates[this.counter][1] = coordinate.Element("initial").Value;
    this.coordinates[this.counter][2] = coordinate.Element("final").Value;
    this.counter++;
};

但现在我收到此错误:
对象引用未设置为对象的实例."

but now I get this error:
"Object reference not set to an instance of an object."

XML

<?xml version="1.0" encoding="utf-8"?>
<grid>
  <coordinate time="62">
    <initial>540:672</initial>
    <final>540:672</final>
  </coordinate>

  ...

  <coordinate time="46">
    <initial>176:605</initial>
    <final>181:617</final>
  </coordinate>
</grid>

跳过了一些坐标标签以适应,但它们都有时间属性和初始/最终子标签.

Skipped a few coordinate tags to fit, but they all had the time attribute and initial/final subtags.

全局

uint counter = 0;

        // Coordinates to be retrieved from the XML file.
        string[][] coordinates;

推荐答案

你可能想检查一下 Linq-to-XML 之类的东西:

You might want to check into something like Linq-to-XML:

XDocument coordinates = XDocument.Load("yourfilename.xml");

foreach(var coordinate in coordinates.Descendants("coordinate"))
{
    string time = coordinate.Attribute("time").Value;

    string initial = coordinate.Element("initial").Value;
    string final = coordinate.Element("final").Value;

    // do whatever you want to do with those items of information now
}

这应该比直接使用低级 XmlTextReader 容易得多......

That should be a lot easier than using straight low-level XmlTextReader....

请参阅此处这里(或许多其他地方)进行介绍到 Linq-to-XML.

See here or here (or a great many other places) for introductions to Linq-to-XML.

更新:

请试试这个代码 - 如果它有效,并且你得到了结果列表中的所有坐标,那么 Linq-to-XML 代码就可以了:

please try this code - if it works, and you get all the coordinates in that resulting list, then the Linq-to-XML code is fine:

定义一个新的辅助类:

public class Coordinate
{
    public string Time { get; set; }
    public string Initial { get; set; }
    public string Final { get; set; }
}

在你的主代码中:

XDocument xdoc = XDocument.Load("C:\test.xml");
IEnumerable<XElement> cords= xdoc.Descendants("coordinate");

var coordinates = cords
                  .Select(x => new Coordinate()
                                   {
                                      Time = x.Attribute("time").Value,
                                      Initial = x.Attribute("initial").Value,
                                      Final = x.Attribute("final").Value
                                    });

这个列表及其内容是什么样的?你得到所有你期望的坐标了吗??

How does this list and its contents look like?? Do you get all the coordinates you're expecting??

这篇关于C# Foreach XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆