解析XML数据转换成在C#中的数组 [英] Parse xml data into an array in C#

查看:143
本文介绍了解析XML数据转换成在C#中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#解析XML数据到一个数组中?

 <详情>
 <排>
  <变量名称=年值=&放大器; QUOT; 2008&安培; QUOT; />
  <变量名称=人的价值=&放大器; QUOT; 10202&安培; QUOT; />
 < /行>
 <排>
  <变量名称=年值=&放大器; QUOT; 2007&安培; QUOT; />
  <变量名称=人的价值=&放大器; QUOT; 11202&安培; QUOT; />
 < /行>
< /细节>


解决方案

从这个例子中,它看起来像你对我都希望得到这样的:

  VAR行= XDocument.Load(filename.xml中)
                    .Root.Elements()
                    。选择(行=> row.Elements()
                                      .ToDictionary(V =方式> v.Attribute(name)的价值,
                                                    V => 。v.Attribute(值)值);

这将创建一个的IEnumerable<字典<字符串,字符串>> ,其中在 IEnumerable的每个条目是行,并在内部字典的每个条目是一个变量。


如果你正在寻找创造一个更加强类型的解决方案,并能保证XML结构通常是两个变量,一个名为 budgetYear 和一个名为帐户,那么像这样的工作:

  //也许需要一个更好的名字。
结构YearAndAccount
{
    私人只读INT budgetYear;
    私人只读字符串帐户ID;    公众诠释BudgetYear {{返回this.budgetYear; }}
    公共字符串ACCOUNTID {{返回this.accountId; }}    公共YearAndAccount(INT budgetYear,串帐户ID)
    {
        this.budgetYear = budgetYear;
        this.accountId =帐户ID;
    }
}VAR行= XDocument.Load(filename.xml中)
                    .Root.Elements()
                    。选择(行=>新建YearAndAccount(
                        。int.Parse(row.Elements()单(EL = GT; el.Attribute(name)的值==budgetYear)。
                                                      属性约(值)。值)
                        。row.Elements()单(EL = GT; el.Attribute(name)的值==账户)。
                                      属性约(值)值))。

这将创建一个的IEnumerable< YearAndAccount方式>


解析code为强类型的解决方案是如此惊人的恶心,因为你的XML是很有条理很差;一个更好的结构将类似于

 <详情>
 < yearAndAccount>
   < budgetYear> 2008< / budgetYear>
   <账户> 10202< /账户>
 < / yearAndAccount>
 < yearAndAccount>
   < budgetYear> 2007< / budgetYear>
   <账户> 11202< /账户>
 < / yearAndAccount>
< /细节>

在这种情况下,code,简直是

  VAR行= XDocument.Load(filename.xml中)
                    .Root.Elements()
                    。选择(行=方式>新YearAndAccount(row.Element(budgetYear)值,
                                                      int.Parse(row.Element(账户)值。)));

How can I parse XML data into an array using C#?

<details> 
 <row> 
  <var name="Year" value="&quot;2008&quot;" /> 
  <var name="person" value="&quot;10202&quot;" /> 
 </row> 
 <row> 
  <var name="Year" value="&quot;2007&quot;" /> 
  <var name="person" value="&quot;11202&quot;" /> 
 </row> 
</details> 

解决方案

From the example, it looks to me like you are hoping to get this:

var rows = XDocument.Load("filename.xml")
                    .Root.Elements()
                    .Select(row => row.Elements()
                                      .ToDictionary(v => v.Attribute("name").Value,
                                                    v => v.Attribute("value").Value);

This will create an IEnumerable<Dictionary<string, string>>, where each entry in the IEnumerable is a "row" and each entry in the inner dictionaries is a "variable".


If you are looking to create a more strongly-typed solution, and can guarantee the XML structure is always two variables, one named budgetYear and one named account, then something like this would work:

// Probably needs a better name.
struct YearAndAccount
{
    private readonly int budgetYear;
    private readonly string accountId;

    public int BudgetYear { get { return this.budgetYear; } }
    public string AccountId { get { return this.accountId; } }

    public YearAndAccount(int budgetYear, string accountId)
    {
        this.budgetYear = budgetYear;
        this.accountId = accountId;
    }
}

var rows = XDocument.Load("filename.xml")
                    .Root.Elements()
                    .Select(row => new YearAndAccount(
                        int.Parse(row.Elements().Single(el => el.Attribute("name").Value == "budgetYear")
                                                      .Attribute("value").Value),
                        row.Elements().Single(el => el.Attribute("name").Value == "account")
                                      .Attribute("value").Value));

This will create an IEnumerable<YearAndAccount>.


The parsing code for the strongly-typed solution is so amazingly icky because your XML is very poorly structured; a better structure would be something like

<details>
 <yearAndAccount>
   <budgetYear>2008</budgetYear>
   <account>10202</account>
 </yearAndAccount>
 <yearAndAccount>
   <budgetYear>2007</budgetYear>
   <account>11202</account>
 </yearAndAccount>
</details>

in which case the code would simply be

var rows = XDocument.Load("filename.xml")
                    .Root.Elements()
                    .Select(row => new YearAndAccount(row.Element("budgetYear").Value,
                                                      int.Parse(row.Element("account").Value)));

这篇关于解析XML数据转换成在C#中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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