C#遍历XmlDocument对象所有节点名称、类型、属性(Attribute)
代码
static void Main(string[] args)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load("../../UpdateActivity2.xml");//doc.Load(文件):从文件加载 doc。doc.LoadXml(字符串)从字符串加载
UpdateActivity2(doc);
}
/// <summary>
/// 测试方法
/// </summary>
/// <param name="doc">XmlDocument对象</param>
public static void UpdateActivity2(XmlDocument doc)
{
string xx = doc.InnerXml;
//Activity2
string activity2ID = "";
string docid = "";
string docTypeid = "";
#region MyRegion
//获取delete的节点
//doc.SelectNodes("//Relationships/Item[@action='delete' and @type='Activity2 Deliverable']/related_id[@type='Deliverable']/Item[@type='Document' or @type='CAD']")
//获取add的节点
//doc.SelectNodes("//Relationships/Item[@action='add' and @type='Activity2 Deliverable']")
#endregion
XmlNodeList Activity2Nodes = doc.SelectNodes("//Item[@type='Activity2']");
////遍历Activity2Nodes中的所有节点
//foreach (XmlNode node in Activity2Nodes)
//{
// Console.WriteLine("=========="+node.Name+"===========");
// foreach (XmlAttribute atr in node.Attributes)
// {
// Console.WriteLine(atr.Name+ "\t:" +atr.Value);
// }
// XmlNodeList childNodes = doc.ChildNodes;
// PrintChildNodes(childNodes);
// Console.Read();
//}
PrintChildNodes(Activity2Nodes);
Console.WriteLine("=============================deleteNodes=======================================");
foreach (XmlNode activity2 in Activity2Nodes)
{
activity2ID = activity2.Attributes["id"].Value;
//Action = delete
XmlNodeList deleteNodes = doc.SelectNodes("//Relationships/Item[@action='delete' and @type='Activity2 Deliverable']/related_id[@type='Deliverable']/Item[@type='Document' or @type='CAD']");
PrintChildNodes(deleteNodes);
}
}
/// <summary>
/// 遍历所有节点的type、name、Attributes
/// </summary>
/// <param name="childnodelist"></param>
static void PrintChildNodes(XmlNodeList childnodelist)
{
try
{
foreach (XmlNode node in childnodelist)
{
//(node.NodeType 是Text时,即是最内层 即innertext值,node.Attributes为null。
if (node.NodeType == XmlNodeType.Text)
{
Console.WriteLine("NodeType:" + node.NodeType + "\t" + node.Name + "\t:" + node.Value);
continue;
}
Console.WriteLine("==========node.Name:" + node.Name + "===========");
foreach (XmlAttribute atr in node.Attributes)
{
Console.WriteLine("NodeType:"+atr.NodeType+"\t"+ atr.Name + "\t:" + atr.Value);
}
if (node.ChildNodes.Count > 0)
{
PrintChildNodes(node.ChildNodes);
}
}
}
catch (Exception ex )
{
Console.WriteLine(ex.Message);
}
}
XML档
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<ApplyItem>
<Item type="Method" action="UpdateActivity2" impersonate="0">
<Item type="Activity2" typeId="E236DB697E294C46B630C99A0D20C2B1" id="6BDF5F2096F14405BAD77A7182DEF34B" action="UpdateActivity" isDirty="1">
<config_id keyed_name="治具图" type="Activity2">6BDF5F2096F14405BAD77A7182DEF34B</config_id>
<created_by_id keyed_name="王旧" type="User">1DCA0BEA51AA46D8982A872C13B0C2C2</created_by_id>
<created_on>2016-10-14T12:07:17</created_on>
<current_state keyed_name="Active" type="Life Cycle State" name="Active">348A09DDD3E34CAFA837984AD1426D9C</current_state>
<date_activated>2017-02-28T19:18:21</date_activated>
<date_due_sched>2017-04-18T00:00:00</date_due_sched>
<date_ef>2017-03-03T00:00:00</date_ef>
<date_es>2017-02-20T00:00:00</date_es>
<date_lf>2017-09-18T00:00:00</date_lf>
<date_ls>2017-09-05T00:00:00</date_ls>
<date_start_sched>2017-04-05T00:00:00</date_start_sched>
<deliv_required>1</deliv_required>
<earned_value>0</earned_value>
<expected_duration>10</expected_duration>
<generation>1</generation>
<id keyed_name="治具图" type="Activity2">6BDF5F2096F14405BAD77A7182DEF34B</id>
<is_critical>0</is_critical>
<is_current>1</is_current>
<is_milestone>0</is_milestone>
<is_released>0</is_released>
<is_required>1</is_required>
<keyed_name>治具图</keyed_name>
<lead_role>Electronics Engineer</lead_role>
<lx_allreview/>
<lx_ph>2</lx_ph>
<lx_requirepart>0</lx_requirepart>
<lx_review>1</lx_review>
<lx_review_pers/>
<major_rev>A</major_rev>
<managed_by_id keyed_name="王 旧" type="Identity">E96B522DF5DF4E19BF27EF915EE4E6AA</managed_by_id>
<modified_by_id keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</modified_by_id>
<modified_on>2017-09-26T19:42:22</modified_on>
<new_version>1</new_version>
<not_lockable>0</not_lockable>
<owned_by_id keyed_name="梁新" type="Identity">FB0D355A16B04A848417EE270BFE0B02</owned_by_id>
<percent_compl>0</percent_compl>
<planned_value>920</planned_value>
<prev_item>A52478EF81F047D7AFF9BAED2B6CE3F1</prev_item>
<proj_num>23343</proj_num>
<signoff_required>0</signoff_required>
<spi>0.0000</spi>
<state>Active</state>
<status>#FF0000</status>
<team_id keyed_name="5CD06953B4354239AA5E00ADEDE7B078" type="Team">D4ECE5BD68B54D969B87C17C068F3E09</team_id>
<name>治具图</name>
<fed_css>
.percent_compl{background-color:#FFCCCC}
.date_start_act{background-color:#FFCCCC}
</fed_css>
<Relationships>
<Item id="45B304A7BF0F4077B44351E1BD87DCBA" type=