2012-05-21 60 views
-3
<Peoples> 
<People> 
    <Name>RadheyJang</Name> 
    <Location>India</Location> 
    <Work>Software Developer</Work> 
    <Point>5</Point> 
    <details> 
    <People> 
    <Name>ArunaTiwari</Name> 
    <Location>India</Location> 
    <Work>SoFtwareCoder</Work> 
    <Point>3</Point> 
    <details/> 
    <Test>A</Test> 
    </People> 
    </details> 
    <Test>NA</Test>  
</People> 
</Peoples> 

我能夠通過使用下面的代碼讀取Xml。只讀部分的XML文件

     XDocument xmlDoc = XDocument.Load(str); 
         var vrresult = from a in xmlDoc.Descendants("People") 
         select new 
         { 
          Name= a.Element("Name").Value, 
          Location= a.Element("Location").Value, 
          Point= a.Element("Point").Value 
         }; 

         GridView1.DataSource = vrresult; 
         GridView1.DataBind(); 

但它正在閱讀詳細內容。我想跳過內的內容元素。請讓我知道如何跳過細節內的內容。

+0

我以前從來沒見過......該錯誤信息,你可以請編輯您的答案,並添加GridView的標記,並結合了您的代碼XML? – McGarnagle

+0

沒有Gridview綁定的問題。我會綁定但在閱讀時綁定它給錯誤。 –

+0

啊,呃。我沒有給你答案,但是這個鏈接可能有幫助:http://www.dotnet247.com/247reference/msgs/5/26129.aspx – McGarnagle

回答

0
var ele = XElement.Parse(xml); 
    // change to XElement.Load if loading from file 
    var result = ele.Descendants("Section").Zip(ele.Descendannt("Mark"), (s,m) => new {Section = s.Value, Mark = m.Value}); Now you can create your DataTable: 

    var table = new DataTable(); 
    var marks = new DataColumn("Mark"); 
    var sections = new  DataColumn("Sections"); 
    table.Columns.Add(marks); table.Columns.Add(sections); 
    foreach (var item in result) 
    { 
    var row = table.NewRow(); 
    row["Mark"] = item.Mark;  
    row["Sections"] = item.Section; 
    table.Rows.Add(row); 
    } 

試試這個代碼..

+0

我嘗試過使用此代碼但我想要讀取3個值您只讀取2個值。請讓我知道如何閱讀3個值和跳過細節屬性。 –

0

我能想到的唯一的事情是,XML是不合法的:

<Peoples> 
<People> *You have an opening People tag here* 
    <Name>RadheyJang</Name> 
    <Location>India</Location> 
    <Work>Software Developer</Work> 
    <Point>5</Point> 
    <details> *You create a details tag here* 
    <People> *You generate the same tag inside of another People tag* 
    <Name>ArunaTiwari</Name> 
    <Location>India</Location> 
    <Work>SoFtwareCoder</Work> 
    <Point>3</Point> 
    <details/> *Then you close the details tag here* 
    <Test>A</Test> 
    </People> 
    </details> *Then you close another one, but there is not a second opening detail tag* 
    <Test>NA</Test>  
</People> 
</Peoples> 

我不知道如果這有助於在所有,但你可能想考慮修正你的XML。

+0

It it system生成XMl文件。 –

+0

@rama不過,如果你的一個應用程序正在創建它,那麼你可能想要改變它的生成方式。 – 3aw5TZetdf

+0

@MatthewRz他沒有關閉'

'標籤和'
'。這是一個空的'
'標籤。標籤的結束是用'
' – Default

1

您需要使用這個XPath ...

using System.Xml.XPath; 

string xml = @" 
    <Peoples> 
     <People> 
     <Name>RadheyJang</Name> 
     <Location>India</Location> 
     <Work>Software Developer</Work> 
     <Point>5</Point> 
     <details> 
      <People> 
      <Name>ArunaTiwari</Name> 
      <Location>India</Location> 
      <Work>SoFtwareCoder</Work> 
      <Point>3</Point> 
      <details/> 
      <Test>A</Test> 
      </People> 
     </details> 
     <Test>NA</Test>  
     </People> 
    </Peoples>"; 

XDocument xmlDoc = XDocument.Parse(xml); 

var vrresult = from a in xmlDoc.XPathSelectElements("/Peoples/People") 
       select new 
       { 
        Name = a.Element("Name").Value, 
        Location = a.Element("Location").Value, 
        Point = a.Element("Point").Value 
       }; 
+0

Alfonso Harita @ working Fine。 –