结构如下
<FEATURE>
<NAME>我的</NAME>
<FEATUREVALUES>
<FEATUREVALUE>小说1</FEATUREVALUE>
<FEATUREVALUE>小说2</FEATUREVALUE>
<FEATUREVALUE>小说3</FEATUREVALUE>
</FEATUREVALUES>
</FEATURE>
<FEATURE>
<NAME>小王的</NAME>
<FEATUREVALUES>
<FEATUREVALUE>小说4</FEATUREVALUE>
<FEATUREVALUE>小说5</FEATUREVALUE>
<FEATUREVALUE>小说6</FEATUREVALUE>
</FEATUREVALUES>
</FEATURE>想得到的效果
姓名 拥有的小说
我的 小说1,小说2,小说3
小王的 小说4,小说5,小说6
<FEATURE>
<NAME>我的</NAME>
<FEATUREVALUES>
<FEATUREVALUE>小说1</FEATUREVALUE>
<FEATUREVALUE>小说2</FEATUREVALUE>
<FEATUREVALUE>小说3</FEATUREVALUE>
</FEATUREVALUES>
</FEATURE>
<FEATURE>
<NAME>小王的</NAME>
<FEATUREVALUES>
<FEATUREVALUE>小说4</FEATUREVALUE>
<FEATUREVALUE>小说5</FEATUREVALUE>
<FEATUREVALUE>小说6</FEATUREVALUE>
</FEATUREVALUES>
</FEATURE>想得到的效果
姓名 拥有的小说
我的 小说1,小说2,小说3
小王的 小说4,小说5,小说6
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("data.xml"));
XmlNode root = doc.DocumentElement;
XmlNode nodeList = root.ChildNodes;
for(int i=0; i<nodeList.length; i++)
{
string name = nodeList[i].SelectSingleNode("name").innerHTML;
nodeList[i].ChildNodes
}
}
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>using System;
using System.IO;
using System.Xml;public class Sample { public static void Main() { XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml"); XmlNodeList nodeList;
XmlNode root = doc.DocumentElement; nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']");
//Change the price on the books.
foreach (XmlNode book in nodeList)
{
book.LastChild.InnerText="15.95";
} Console.WriteLine("Display the modified XML document....");
doc.Save(Console.Out);
}
}
参考一下了
可以使用xml的xpath试试
public static DataTable Fun(string fileName)
{
DataTable dt = new DataTable();
DataColumn c1 = new DataColumn("name", typeof(string));
DataColumn c2 = new DataColumn("book", typeof(string));
dt.Columns.Add(c1);
dt.Columns.Add(c2); XmlDocument doc = new XmlDocument();
doc.Load(fileName);
//获取所有的FEATURE节点,然后遍历
XmlNodeList nodes=doc.SelectNodes("//FEATURE");
foreach (XmlNode node in nodes)
{
DataRow row = dt.NewRow();
//获取name节点
string name = node.SelectSingleNode("NAME").InnerText;
row[0] = name; //获取FEATUREVALUE节点
XmlNodeList books = node.SelectNodes("FEATUREVALUES/FEATUREVALUE");
string bookName = "";
foreach (XmlNode book in books)
{
bookName += book.InnerText + ";";
}
row[1]=bookName;
dt.Rows.Add(row);
} return dt;
}