需要有关在.NET 4中解析XML的信息

时间:2011-12-01 23:10:50

标签: c# .net xml linq-to-xml

我对XML很新,并使用C#解析它。我试图解析一个看起来像这样的XML文档:

<Msg Version="1.0">
  <ProgList>
    <update sequence="41248" amount="3327" Lvl="1" Grp="1" />
    <update sequence="41216" amount="5326" Lvl="2" Grp="1" />
    <update sequence="41252" amount="7326" Lvl="3" Grp="1" />
  </ProgList>
</Msg>

我正在尝试使用XDocument,似乎无法使用属性解析这三个更新。

但是,如果我以这种形式创建包含相同数据的XML文档:

<Msg Version="1.0">
    <ProgList>
        <Level>
            <seq>41248</seq>
            <amount>3327</amount>
            <Lvl>1</Lvl>
            <Grp>1</Grp>
        </Level>
        <Level>
            <seq>41216</seq>
            <amount>5326</amount>
            <Lvl>2</Lvl>
            <Grp>1</Grp>
        </Level>
        <Level>
            <seq>41252</seq>
            <amount>7326</amount>
            <Lvl>3</Lvl>
            <Grp>1</Grp>
        </Level>
    </ProgList>
</Msg>

我可以使用以下代码解析和迭代Level数据:

String xmlText = String.Empty;
//String xml = String.Empty;
int begin = Data.IndexOf("<?xml");
int end = Data.IndexOf("</Msg") + 6;

try
{
    //xml = Data.Substring(begin, end - begin);
}
catch { };

String xml = 
@"<Msg Version='1.0'>
                                <ProgList>
                                    <Level>
                                        <seq>41248</seq>
                                        <amount>3327</amount>
                                        <Lvl>1</Lvl>
                                        <Grp>1</Grp>
                                    </Level>
                                    <Level>
                                        <seq>41216</seq>
                                        <amount>5326</amount>
                                        <Lvl>2</Lvl>
                                        <Grp>1</Grp>
                                    </Level>
                                    <Level>
                                        <seq>41252</seq>
                                        <amount>7326</amount>
                                        <Lvl>3</Lvl>
                                        <Grp>1</Grp>
                                    </Level>
                                </ProgList>
</Msg>";



try
{
    XDocument xmlDoc = XDocument.Parse(xml);

    var updates = from x in xmlDoc.Descendants("Level")
                    select new
                    {
                        seq = x.Descendants("seq").First().Value,
                        amt = x.Descendants( "amount" ).First().Value,
                        lvl = x.Descendants("Lvl").First().Value,
                        grp = x.Descendants("Grp").First().Value
                    };

    foreach (var x in updates)
    {
        String output = String.Format("{0}SEQ: {1} AMT: {2} LVL: {3} GRP: {4}",
                                Environment.NewLine, x.seq, x.amt, x.lvl, x.grp);
    }

我如何解析第一个XML文档样式?

1 个答案:

答案 0 :(得分:6)

使用Attribute Method代替Descendants Method

var updates = from x in xmlDoc.Root.Element("ProgList").Elements("update")
              select new
              {
                  seq = (int)x.Attribute("sequence"),
                  amt = (int)x.Attribute("amount"),
                  lvl = (int)x.Attribute("Lvl"),
                  grp = (int)x.Attribute("Grp"),
              };