I am trying to f开发者_如何学编程ind some working examples to learn the wonders on Dataset with XML. Im using this example of the xml data. I want to search through all the CD nodes for the TITLE value.
DataSet dsXml = new DataSet();
dsXml.ReadXml(msXml);
look at using linq2xml. you could also use linq to "query" the dataset. http://msdn.microsoft.com/en-us/vbasic/bb688086.aspx
Here is a very simple C# code which will print all the "TITLE" in the provided XML:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataSet dsXml = new DataSet();
dsXml.ReadXml("http://www.w3schools.com/xml/cd_catalog.xml");
for (int i = 0; i < dsXml.Tables.Count; i++)
{
Console.WriteLine("Table Name: " + dsXml.Tables[i].TableName);
int j = 1;
foreach (DataRow myRow in dsXml.Tables[i].Rows)
{
Console.Write("[" + j++ + "]");
foreach (DataColumn myColumn in dsXml.Tables[i].Columns)
{
if (myColumn.ColumnName.Equals("TITLE"))
Console.Write("[" + myRow[myColumn] + "]");
}
Console.WriteLine("");
}
}
}
}
}
精彩评论