首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

LINQ to xml,该怎么解决

2012-03-07 
LINQto xmlprice.xmlXML code?xml version1.0 encodinggb2312 ?rootpricepric01/pricpri

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

热点排行