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

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

问题描述

我将XML文件的二维坐标保存为一个类似于以下内容的结构:

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

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

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






新代码:



我的新代码:

  //读取XML文件。 
XDocument xmlDoc = XDocument.Load(C:\\test.xml);

foreach(xmlDoc.Descendants(coordinate)中的坐标)
{
this.coordinates [this.counter] [0] = coordinate.Attribute(time )。值;
this.coordinates [this.counter] [1] = coordinate.Element(initial)。Value;
this.coordinates [this.counter] [2] = coordinate.Element(final)。Value;
this.counter ++;
};

但是现在我得到这个错误:

Object reference not set to an






XML

 <?xml version =1.0encoding =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>

跳过了几个坐标标签,但它们都具有时间属性和初始/最终子标签。






全局

  uint counter = 0; 

//从XML文件中检索坐标。
string [] []坐标;


解决方案

您可能想要检查Linq- -XML:

  XDocument coordinates = XDocument.Load(yourfilename.xml); 
$ b foreach(坐标中的坐标.Descendants(坐标))
{
string time = coordinate.Attribute(time)。Value;

字符串初始值= coordinate.Element(initial)。Value;
string final = coordinate.Element(final)。Value;

//对这些信息做任何你想做的事情现在

$ / code $ / pre

这应该比使用直接的低级别XmlTextReader更容易......

请参阅 here 这里(或许多其他地方)介绍Linq-to-XML。




更新:

请尝试此代码 - ,然后你得到结果列表中的所有坐标,那么Linq-to-XML代码就可以了:



定义一个新的helper类:

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

以及您的主要代码:

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

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

这份清单及其内容如何?你会得到你期望的所有坐标吗?


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>

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;


New Code:

My New Code:

//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.


Globals

uint counter = 0;

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

解决方案

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
}

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

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


UPDATE:

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:

Define a new helper class:

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

and in your main code:

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天全站免登陆