/////////////////////XML Begin////////////////////
<list> <list1>
<type>AA</type>
<method>download</method>
<isDelete>true</isDelete>
<ftpServer>127.0.0.1</ftpServer>
<ftpPort>21</ftpPort>
<ftpUser>test</ftpUser>
<ftpPassword>test</ftpPassword>
<source>X:\</source>
<destination>customs</destination>
<LogPath>./test</LogPath>
</list1> <list2>
<type>BB</type>
<method>download</method>
<isDelete>true</isDelete>
<ftpServer>127.0.0.1</ftpServer>
<ftpPort>21</ftpPort>
<ftpUser>test</ftpUser>
<ftpPassword>test</ftpPassword>
<source>X:\</source>
<destination>customs</destination>
<LogPath>./test</LogPath>
</list2></list>/////////////////////XML END/////////////////////////////////////上面是一段XML,我想遍历List*,然后读写每个List*下面的各个值,请问用C#怎么实现?
<list> <list1>
<type>AA</type>
<method>download</method>
<isDelete>true</isDelete>
<ftpServer>127.0.0.1</ftpServer>
<ftpPort>21</ftpPort>
<ftpUser>test</ftpUser>
<ftpPassword>test</ftpPassword>
<source>X:\</source>
<destination>customs</destination>
<LogPath>./test</LogPath>
</list1> <list2>
<type>BB</type>
<method>download</method>
<isDelete>true</isDelete>
<ftpServer>127.0.0.1</ftpServer>
<ftpPort>21</ftpPort>
<ftpUser>test</ftpUser>
<ftpPassword>test</ftpPassword>
<source>X:\</source>
<destination>customs</destination>
<LogPath>./test</LogPath>
</list2></list>/////////////////////XML END/////////////////////////////////////上面是一段XML,我想遍历List*,然后读写每个List*下面的各个值,请问用C#怎么实现?
doc.Load("test.xml"); XmlNodeList node = doc.SelectNodes("/list/*");//取得list下面的所有节点
foreach (XmlNode n in node)
{
if (n.ChildNodes.Count > 0)//判断,也就是list1,list2等节点
{
n.ChildNodes[0].InnerText;//type element
n.ChildNodes[1].InnerText;//method element
}
}
大概这个意思
http://msdn.microsoft.com/zh-cn/library/182eeyhh(VS.80).aspx
不过确实比较简单~
hoho
{
private DataSet ds = new DataSet();
private string mPath = ""; public DataSet getDS()
{
return ds;
} /// <summary>
/// 加载获取的MXL 获取XML路径
/// </summary>
/// <param name="path"></param>
public void LoadSetting(string path)
{
this.mPath = path;
this.ds = new DataSet(); try
{
ds.ReadXml(@mPath);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
}
} public DataTable Getting(string fNode)
{
DataTable dt = new DataTable(); try
{
dt = ds.Tables[fNode];
return dt; }
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
throw; }
} /// <summary>
/// 获取fNode父节点下第intKey行 strSection的属性值
/// </summary>
/// <param name="fNode">父节电名称</param>
/// <param name="intKey">行数</param>
/// <param name="strSection">属性名称</param>
/// <returns></returns>
public string Getting(string fNode, int intKey, string strSection)
{
string strValue = ""; try
{
strValue = Convert.ToString(ds.Tables[fNode].Rows[intKey][strSection]);
return strValue; }
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
}
} /// <summary>
/// 把ds写到路径mPath下,保存成XML
/// </summary>
/// <param name="ds1"></param>
/// <param name="mPath1"></param>
public void SaveSettings(DataSet ds1, string mPath1)
{
try
{
ds1.WriteXml(@mPath1);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
}
} /// <summary>
/// 更改父节点为fNode 第intKey行 strSection属性值 为strDefaultValue
/// </summary>
/// <param name="fNode">父节点名称</param>
/// <param name="intKey">行数</param>
/// <param name="strSection">属性名称</param>
/// <param name="strDefaultValue">更改的值</param>
public void SetSetting(string fNode, int intKey, string strSection, string strDefaultValue)
{
try
{
ds.Tables[fNode].Rows[intKey][strSection] = strDefaultValue;
ds.AcceptChanges();
this.SaveSettings(ds,mPath); }
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
} } /// <summary>
/// 在父节点为fNode intKey行 添加 属性名称strSection 值strDefaultValue
/// </summary>
/// <param name="fNode">父节点名称</param>
/// <param name="intKey">行数</param>
/// <param name="strSection">添加的属性名称</param>
/// <param name="strDefaultValue">属性值</param>
public void AddSettings(string fNode, int intKey, string strSection, string strDefaultValue)
{
try
{
ds.Tables[fNode].Columns.Add(strSection);
ds.Tables[fNode].Rows[intKey][strSection] = strDefaultValue;
ds.AcceptChanges();
this.SaveSettings(ds, mPath);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
}
} /// <summary>
/// 删除父节点为fNode下的 属性值strSection
/// </summary>
/// <param name="fNode"></param>
/// <param name="strSection"></param>
public void DeleteSettings(string fNode, string strSection)
{
try
{
ds.Tables[fNode].Columns.Remove(strSection);
ds.AcceptChanges();
this.SaveSettings(ds, mPath);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
}
}
}
DataSet ds = new DataSet();
ds.Tables.Count;