下面代碼:
public DataView GetData(string XmlPathNode)
{
//Get XML Date,return DataView
try
{
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
}
catch
{
objError.ProgramError();
}
if ( ds.Tables.Count == 0 )
{
return new DataView();
}
return ds.Tables[0].DefaultView;
}
{
//Get XML Date,return DataView
try
{
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
}
catch
{
objError.ProgramError();
}
if ( ds.Tables.Count == 0 )
{
return new DataView();
}
return ds.Tables[0].DefaultView;
}
此方法是讀取XML數據中指定節點及值。後來在一位朋友改寫了下代碼:
public DataView GetData(string XmlPathNode)
{
//Get XML Date,return DataView
try
{
// System.IO.StringReader read = new System.IO.StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
XmlNodeList xnl = objXmlDoc.GetElementsByTagName(XmlPathNode);
System.Text.StringBuilder strXml = new System.Text.StringBuilder();
strXml.Append( "<?xml version='1.0' ?><root>" );
for (int i=0; i<xnl.Count; i++)
{
strXml.Append( xnl[i].OuterXml );
}
strXml.Append( "</root>" );
System.IO.StringReader read = new System.IO.StringReader( strXml.ToString() );
ds.ReadXml(read);
}
catch
{
//
}
return ds.Tables[0].DefaultView;
}
{
//Get XML Date,return DataView
try
{
// System.IO.StringReader read = new System.IO.StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
XmlNodeList xnl = objXmlDoc.GetElementsByTagName(XmlPathNode);
System.Text.StringBuilder strXml = new System.Text.StringBuilder();
strXml.Append( "<?xml version='1.0' ?><root>" );
for (int i=0; i<xnl.Count; i++)
{
strXml.Append( xnl[i].OuterXml );
}
strXml.Append( "</root>" );
System.IO.StringReader read = new System.IO.StringReader( strXml.ToString() );
ds.ReadXml(read);
}
catch
{
//
}
return ds.Tables[0].DefaultView;
}
此方法是讀取所有XmlPathNode節點及值,重新生成一個XML,返回DataView。
兩方法的區別在於:前者讀取指定節點,後者是讀所有節點。