只读XML文件的一部分
RadheyJang India Software Developer 5 ArunaTiwari India SoFtwareCoder 3 A NA
我可以使用下面的代码读取那个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();
但它正在阅读细节的内容。 我想跳过阅读详细信息元素内的内容 。 请让我知道如何跳过详细内容。
你需要使用XPath …
using System.Xml.XPath; string xml = @" RadheyJang India Software Developer 5 ArunaTiwari India SoFtwareCoder 3 A NA "; 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 };
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); }
试试这个代码..
我唯一能想到的是XML不合法:
*You have an opening People tag here* RadheyJang India Software Developer 5 *You create a details tag here* *You generate the same tag inside of another People tag* ArunaTiwari India SoFtwareCoder 3 *Then you close the details tag here* A *Then you close another one, but there is not a second opening detail tag* NA
我不确定这是否有帮助,但您可能需要考虑修复XML。