日期:2014-05-20  浏览次数:20889 次

LINQ to xml
price.xml
XML code

<?xml version="1.0" encoding="gb2312" ?>
<root>
<price>
    <pric>01</pric>
    <pri>01</pri>
</price>

<price>
    <pric>02</pric>
    <pri>02</pri>
</price>

<price>
    <pric>03</pric>
    <pri>03</pri>
</price>

<price>
    <pric>04</pric>
    <pri>04</pri>
</price>
</root>



name.xml
XML code

<?xml version="1.0" encoding="gb2312" ?>
<root>
<price>
    <pri>i01</pri>
    <name>第一个</name>
</price>

<price>
    <pri>02</pri>
    <name>第二个</name>
</price>
<price>
    <pri>03</pri>
    <name>第三个</name>
</price>
<price>
    <pri>04</pri>
    <name>第四个</name>
</price>

</root>


C# code

        static void Excute()
        {
            XDocument xd = XDocument.Load("name.xml");
            XDocument dx = XDocument.Load("price.xml");
            var result = from x in xd.Elements()
                         join d in dx.Elements()
                         on x.Element("pri").Value equals d.Element("pri").Value
                         select new
                         {
                             pre = d.Element("pric").Value,
                             name = x.Element("name").Value
                         };
            foreach (var o in result)
            {
                Console.WriteLine(o.name + " " + o.pre);
            }                  
        }


代码写错了吗??

------解决方案--------------------
C# code

static void Excute()
        {
            XDocument xd = XDocument.Load("name.xml");
            XDocument dx = XDocument.Load("price.xml");
            var result = from x in xd.Descendants("price")
                         join d in dx.Descendants("price")
                         on x.Element("pri").Value equals d.Element("pri").Value
                         select new
                         {
                             pre = d.Element("pric").Value,
                             name = x.Element("name").Value
                         };
            foreach (var o in result)
            {
                Console.WriteLine(o.name + " " + o.pre);
            }                  
        }

------解决方案--------------------
C# code

static void Excute()
        {
            XDocument xd = XDocument.Load("name.xml");
            XDocument dx = XDocument.Load("price.xml");
            var result = from x in xd.Descendants("price")
                         join d in dx.Descendants("price")
                         on x.Element("pri").Value equals d.Element("pri").Value
                         select new
                         {
                             pre = d.Element("pric").Value,
                             name = x.Element("name").Value
                         };
            foreach (var o in result)
            {
                Console.WriteLine(o.name + " " + o.pre);
            }                  
        }