读书人

LINQ to xml,该怎么解决

发布时间: 2012-03-07 09:13:51 作者: rapoo

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);            }                          } 

读书人网 >.NET

热点推荐