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="Activity2 Deliverable" typeId="0CCA05E05A104F2E88CF123EF4F5171E" action="delete">
<created_by_id keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</created_by_id>
<created_on>2017-09-26T19:38:16</created_on>
<css is_null="1"/>
<current_state is_null="1"/>
<id keyed_name="45B304A7BF0F4077B44351E1BD87DCBA" type="Activity2 Deliverable">45B304A7BF0F4077B44351E1BD87DCBA</id>
<keyed_name>45B304A7BF0F4077B44351E1BD87DCBA</keyed_name>
<locked_by_id is_null="1"/>
<lx_activity2manager keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</lx_activity2manager>
<lx_activity2name>治具图</lx_activity2name>
<lx_classification>品质文件/不良看板</lx_classification>
<lx_projectnum>23343</lx_projectnum>
<major_rev>A</major_rev>
<modified_by_id keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</modified_by_id>
<modified_on>2017-09-26T19:38:16</modified_on>
<related_id keyed_name="DOC16-0000029605" type="Deliverable">
<Item type="Document" typeId="B88C14B99EF449828C5D926E39EE8B89" id="49A80B2AE016488FB9095B7E6FDA5562">
<classification>品质文件/不良看板</classification>
<config_id keyed_name="DOC16-0000029605" type="Document">49A80B2AE016488FB9095B7E6FDA5562</config_id>
<created_by_id keyed_name="王山" type="User">97CDC886228E4217B70EB32C5E7A051B</created_by_id>
<created_on>2016-12-08T14:39:50</created_on>
<current_state keyed_name="Released" type="Life Cycle State" name="Released">A089866A0FE34B6799296DE32FD23DB0</current_state>
<description>外部线TYPE-C终检不良看板</description>
<effective_date>2016-12-09T09:35:04</effective_date>
<generation>1</generation>
<has_change_pending>0</has_change_pending>
<has_files>1</has_files>
<id keyed_name="DOC16-0000029605" type="Document">49A80B2AE016488FB9095B7E6FDA5562</id>
<is_batchloader>0</is_batchloader>
<is_current>1</is_current>
<is_released>1</is_released>
<is_template>0</is_template>
<keyed_name>DOC16-0000029605</keyed_name>
<lx_commonarea>外部线</lx_commonarea>
<lx_customer/>
<lx_doc_cad_partstype>10</lx_doc_cad_partstype>
<lx_parts/>
<lx_producttype>PTAC_BZLANTO</lx_producttype>
<lx_projectname/>
<lx_projectnamer>test</lx_projectnamer>
<lx_projectnum>23343</lx_projectnum>
<lx_ssss>0</lx_ssss>
<lx_usefactory>BZLANTO</lx_usefactory>
<major_rev>X</major_rev>
<modified_by_id keyed_name="Admin" type="User">30B991F927274FA3829655F50C99472E</modified_by_id>
<modified_on>2016-12-09T09:35:04</modified_on>
<name>外部线TYPE-C终检不良看板</name>
<new_version>0</new_version>
<not_lockable>1</not_lockable>
<owned_by_id keyed_name="品质工程师" type="Identity">7EB1EB96188A438588DDAAD14A2BE716</owned_by_id>
<release_date>2016-12-09T09:35:04</release_date>
<sop_gen>1</sop_gen>
<sop_ver>A</sop_ver>
<state>Released</state>
<item_number>DOC16-0000029605</item_number>
<itemtype>B88C14B99EF449828C5D926E39EE8B89</itemtype>
</Item>
</related_id>
<source_id keyed_name="治具图" type="Activity2">6BDF5F2096F14405BAD77A7182DEF34B</source_id>
</Item>
<Item id="7387AC910646405B8CDFA2B8E031B822" type="Activity2 Deliverable" typeId="0CCA05E05A104F2E88CF123EF4F5171E" action="delete">
<created_by_id keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</created_by_id>
<created_on>2017-09-26T19:39:19</created_on>
<css is_null="1"/>
<current_state is_null="1"/>
<id keyed_name="7387AC910646405B8CDFA2B8E031B822" type="Activity2 Deliverable">7387AC910646405B8CDFA2B8E031B822</id>
<keyed_name>7387AC910646405B8CDFA2B8E031B822</keyed_name>
<locked_by_id is_null="1"/>
<lx_activity2manager keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</lx_activity2manager>
<lx_activity2name>治具图</lx_activity2name>
<lx_classification>工程文件/标准排配</lx_classification>
<lx_projectnum>23343</lx_projectnum>
<major_rev>A</major_rev>
<modified_by_id keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</modified_by_id>
<modified_on>2017-09-26T19:39:19</modified_on>
<related_id keyed_name="DOC16-0000029606" type="Deliverable">
<Item type="Document" typeId="B88C14B99EF449828C5D926E39EE8B89" id="B241B7570448435899F402D48D01599C">
<classification>工程文件/标准排配</classification>
<config_id keyed_name="DOC16-0000029606" type="Document">B241B7570448435899F402D48D01599C</config_id>
<created_by_id keyed_name="黄平" type="User">63E3C06CEC7E429DA6D06ECB87D5B96E</created_by_id>
<created_on>2016-12-08T15:08:59</created_on>
<current_state keyed_name="Released" type="Life Cycle State" name="Released">7726A5CBBACA4E398FA26DA5682AAB4A</current_state>
<effective_date>2016-12-10T13:52:07</effective_date>
<generation>1</generation>
<has_change_pending>0</has_change_pending>
<has_files>1</has_files>
<id keyed_name="DOC16-0000029606" type="Document">B241B7570448435899F402D48D01599C</id>
<is_batchloader>0</is_batchloader>
<is_current>1</is_current>
<is_released>1</is_released>
<is_template>0</is_template>
<keyed_name>DOC16-0000029606</keyed_name>
<lx_customer/>
<lx_parts>LGJSA002-DT-R</lx_parts>
<lx_producttype>PTAA_LX53</lx_producttype>
<lx_projectname/>
<lx_projectnamer>test</lx_projectnamer>
<lx_projectnum>23343</lx_projectnum>
<lx_ssss>0</lx_ssss>
<lx_usefactory>LX53</lx_usefactory>
<major_rev>X</major_rev>
<modified_by_id keyed_name="Admin" type="User">30B991F927274FA3829655F50C99472E</modified_by_id>
<modified_on>2016-12-10T13:52:07</modified_on>
<name>LGJSA002-DT-R排A</name>
<new_version>0</new_version>
<not_lockable>1</not_lockable>
<owned_by_id keyed_name="线装IE工程师" type="Identity">272C18498A9B4902BA7831A8293EEE2D</owned_by_id>
<release_date>2016-12-10T13:52:07</release_date>
<sop_gen>1</sop_gen>
<sop_ver>A</sop_ver>
<state>Released</state>
<item_number>DOC16-0000029606</item_number>
<itemtype>B88C14B99EF449828C5D926E39EE8B89</itemtype>
</Item>
</related_id>
<source_id keyed_name="治具图" type="Activity2">6BDF5F2096F14405BAD77A7182DEF34B</source_id>
</Item>
<Item id="4088E5C4E4594477B74D34C2B36D88FB" type="Activity2 Deliverable" typeId="0CCA05E05A104F2E88CF123EF4F5171E">
<created_by_id keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</created_by_id>
<created_on>2017-09-26T19:42:22</created_on>
<css is_null="1"/>
<current_state is_null="1"/>
<id keyed_name="4088E5C4E4594477B74D34C2B36D88FB" type="Activity2 Deliverable">4088E5C4E4594477B74D34C2B36D88FB</id>
<keyed_name>4088E5C4E4594477B74D34C2B36D88FB</keyed_name>
<locked_by_id is_null="1"/>
<lx_activity2manager keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</lx_activity2manager>
<lx_activity2name>治具图</lx_activity2name>
<lx_classification>工程文件/标准排配</lx_classification>
<lx_projectnum>23343</lx_projectnum>
<major_rev>A</major_rev>
<modified_by_id keyed_name="王 旧" type="User">4C66AA1CBF5145F8B37F37244E84ABFE</modified_by_id>
<modified_on>2017-09-26T19:42:22</modified_on>
<related_id keyed_name="DOC16-0000029607" type="Deliverable">
<Item type="Document" typeId="B88C14B99EF449828C5D926E39EE8B89" id="751623C841794ED3AA6E30BA8731EF97">
<classification>工程文件/标准排配</classification>
<config_id keyed_name="DOC16-0000029607" type="Document">751623C841794ED3AA6E30BA8731EF97</config_id>
<created_by_id keyed_name="黄平" type="User">63E3C06CEC7E429DA6D06ECB87D5B96E</created_by_id>
<created_on>2016-12-08T15:10:52</created_on>
<current_state keyed_name="Released" type="Life Cycle State" name="Released">7726A5CBBACA4E398FA26DA5682AAB4A</current_state>
<effective_date>2016-12-10T13:52:07</effective_date>
<generation>1</generation>
<has_change_pending>0</has_change_pending>
<has_files>1</has_files>
<id keyed_name="DOC16-0000029607" type="Document">751623C841794ED3AA6E30BA8731EF97</id>
<is_batchloader>0</is_batchloader>
<is_current>1</is_current>
<is_released>1</is_released>
<is_template>0</is_template>
<keyed_name>DOC16-0000029607</keyed_name>
<lx_customer/>
<lx_parts>LGJSA001-DT-R</lx_parts>
<lx_producttype>PTAA_LX53</lx_producttype>
<lx_projectname/>
<lx_projectnamer>test,test</lx_projectnamer>
<lx_projectnum>26383,23343</lx_projectnum>
<lx_ssss>0</lx_ssss>
<lx_usearea>A83</lx_usearea>
<lx_usefactory>LX53</lx_usefactory>
<major_rev>X</major_rev>
<modified_by_id keyed_name="Admin" type="User">30B991F927274FA3829655F50C99472E</modified_by_id>
<modified_on>2016-12-10T13:52:07</modified_on>
<name>LGJSA001-DT-R排A</name>
<new_version>0</new_version>
<not_lockable>1</not_lockable>
<owned_by_id keyed_name="线装IE工程师" type="Identity">272C18498A9B4902BA7831A8293EEE2D</owned_by_id>
<release_date>2016-12-10T13:52:07</release_date>
<sop_gen>1</sop_gen>
<sop_ver>A</sop_ver>
<state>Released</state>
<item_number>DOC16-0000029607</item_number>
<itemtype>B88C14B99EF449828C5D926E39EE8B89</itemtype>
</Item>
</related_id>
<source_id keyed_name="治具图" type="Activity2">6BDF5F2096F14405BAD77A7182DEF34B</source_id>
</Item>
<Item type="Activity2 Deliverable" id="34A6671FC78F4C0EBA815F1AD5C4C702" action="add" loaded="1" levels="0" isTemp="1" typeId="0CCA05E05A104F2E88CF123EF4F5171E" LastModifiedOn="1506509992553">
<related_id>
<Item type="Deliverable" typeId="86920FEE161244F6B4B507130B18CADE" id="A93C9EE8656C4BDC8F0B22337B07FC1B" levels="0">
<classification>客户图</classification>
<config_id keyed_name="CAD17-0000011829" type="Deliverable">A93C9EE8656C4BDC8F0B22337B07FC1B</config_id>
<created_by_id keyed_name="梁新" type="User">70EC6C95D08F409E9E2378B02115E572</created_by_id>
<created_on>2017-03-08T14:07:45</created_on>
<current_state keyed_name="In Review" type="Life Cycle State" name="In Review">870C0DCA808F4D0F9718F5F37388235C</current_state>
<generation>1</generation>
<id keyed_name="CAD17-0000011829" type="Deliverable">A93C9EE8656C4BDC8F0B22337B07FC1B</id>
<is_current>1</is_current>
<is_released>0</is_released>
<itemtype>CCF205347C814DD1AF056875E0A880AC</itemtype>
<keyed_name>CAD17-0000011829</keyed_name>
<major_rev>X</major_rev>
<modified_by_id keyed_name="梁新" type="User">70EC6C95D08F409E9E2378B02115E572</modified_by_id>
<modified_on>2017-08-07T10:18:29</modified_on>
<new_version>0</new_version>
<not_lockable>1</not_lockable>
<owned_by_id keyed_name="研发工程师" type="Identity">5BCB3F52640B434EB27747A79D25DB4B</owned_by_id>
<state>In Review</state>
</Item>
</related_id>
<source_id keyed_name="治具图">6BDF5F2096F14405BAD77A7182DEF34B</source_id>
</Item>
<Item type="Activity2 Deliverable" id="06562E228E0B48E19C5D32CB739517DD" action="add" loaded="1" levels="0" isTemp="1" typeId="0CCA05E05A104F2E88CF123EF4F5171E" LastModifiedOn="1506509992699">
<related_id>
<Item type="Deliverable" typeId="86920FEE161244F6B4B507130B18CADE" id="AE6002C6F5F9403F89FD1521E52AE73F" levels="0">
<classification>客户图</classification>
<config_id keyed_name="CAD17-0000011828" type="Deliverable">BD95AA887A9C4241810FA28875491DCB</config_id>
<created_by_id keyed_name="梁新" type="User">70EC6C95D08F409E9E2378B02115E572</created_by_id>
<created_on>2017-03-08T14:07:39</created_on>
<current_state keyed_name="In Review" type="Life Cycle State" name="In Review">870C0DCA808F4D0F9718F5F37388235C</current_state>
<generation>3</generation>
<id keyed_name="CAD17-0000011828" type="Deliverable">AE6002C6F5F9403F89FD1521E52AE73F</id>
<is_current>1</is_current>
<is_released>0</is_released>
<itemtype>CCF205347C814DD1AF056875E0A880AC</itemtype>
<keyed_name>CAD17-0000011828</keyed_name>
<major_rev>X</major_rev>
<modified_by_id keyed_name="梁新" type="User">70EC6C95D08F409E9E2378B02115E572</modified_by_id>
<modified_on>2017-08-07T10:18:29</modified_on>
<new_version>0</new_version>
<not_lockable>1</not_lockable>
<owned_by_id keyed_name="研发工程师" type="Identity">5BCB3F52640B434EB27747A79D25DB4B</owned_by_id>
<state>In Review</state>
</Item>
</related_id>
<source_id keyed_name="治具图">6BDF5F2096F14405BAD77A7182DEF34B</source_id>
</Item>
</Relationships>
<CurrentUserId>4C66AA1CBF5145F8B37F37244E84ABFE</CurrentUserId>
</Item>
</Item>
</ApplyItem>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>