-1

我有以下 XML 文件:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <invoice xmlns="http://www.jemos.co.uk/xsds/experiments">
<header date="2012-12-27T13:17:03.652Z" invoiceId="0yFUWhwmHH">
    <seller name="euxz0bIqiC">
        <address line1="fxWDfaMu8O" line2="u5LmqdrH6f" line3="5c7XVSiXCh"
            city="CyCJOexD1v" postcode="Td4RmntPah" country="_vzVd1oo1Z" />
    </seller>
    <buyer name="UKNWLox8WE">
        <address line1="6RcK4QKz94" line2="U04UEsRMjh" line3="lnSWe9NY0r"
            city="g5nj0tbJbx" postcode="evyno9yUR6" country="rpIgh2XDIo" />
    </buyer>
</header>
<details>
    <items>
        <item id="ZBV9eUw0MS" description="h_x4Lqr2bp" quantity="1356614223664"
            price="0.552495629185551" currency="NdqzH508tA" />
        <item id="HNAQbfAXrv" description="sTgStzKny7" quantity="1356614223666"
            price="0.10527685341195969" currency="tvJUSZjipG" />
        <item id="ikJ0y_8TdZ" description="RUXb6n7cMJ" quantity="1356614223667"
            price="0.8693868918655814" currency="8LHywfSuC7" />
        <item id="lbdLaiI7zK" description="B_kId9Er07" quantity="1356614223668"
            price="0.9610746134524019" currency="bJvcOByXzx" />
        <item id="fBRl89ir8K" description="CuUiyjfFrz" quantity="1356614223669"
            price="0.6002495627735738" currency="WqB4AgH6Jv" />
    </items>
</details>

并得到以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace XMLExperiments
{
class Program
{


    static void Main(string[] args)
    {

        XDocument xml = XDocument.Parse(Properties.Resources.test);
        IEnumerable<XElement> items = xml.Descendants()
            .Where(el => el.Name.LocalName.Contains("item"));

        foreach (XElement item in items)
        {
            IEnumerable<XAttribute> attrs = item.Attributes()
                .OrderBy(at => at.Name.LocalName);                  
            foreach (XAttribute attr in attrs)
               Console.WriteLine("Attribute name: {0}", attr.Name.LocalName);

        }

        Console.ReadKey();
    }
}
}

执行后,我得到以下信息:

在此处输入图像描述

我本来希望“货币”首先出现,而所有其他项目都正确排序。如果我按降序排序,也会发生类似的事情。

任何指针?

4

1 回答 1

6

Based on your output, it looks like it did.

The last few lines are:

currency  
description  
id  
price  
quantity  

In your screen shot there are 4 places where "currency" exists and 5 that "quantity" exists. All things point to the first line simply being scrolled off the top.

于 2012-12-27T16:48:50.527 回答