如何在C#中将xml节点添加到xml文档中的特定位置

时间:2014-11-12 17:31:21

标签: c# xml nodes

XML文件数据

<xml version="1.0" encoding="utf-8"?>
<Module xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Table="" Type="Process Start" ReadOnly="false" xmlns="http://localhost/License2000/xml/ModuleSchema.xsd">
 <Attributes>...</Attributes>
 <Clients>
   <Client Name="*">
     <Processes>
       <Process Name="ren">...</Process>
       <Process Name="dup">...</Process>
       <Process Name="DEM">...</Process>
       <Process Name="LAC">...</Process>
       <Process Name="APP">
         <Agencies>
           <Agency Name="1">
             <Professions>
               <Profession Name="4">
                  <LicenseTypes>
                    <LicenseType Name="202">
                      <Controls Role="*">
                        <Control Visible="true" Type="com.sac.eLicense.Controls.Label" ReadOnly="false" Required="false" Value="&lt;p class=MsoNormal&gt;&lt;b&gt;&lt;span style='font-size:11.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;'&gt;Please&#xD;&#xA;read the following information and requirements for initial licensure as an&#xD;&#xA;associate broker standard carefully before proceeding with the online process.&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&#xD;&#xA;&#xD;&#xA;&lt;p class=MsoNormal&gt;&lt;b&gt;&lt;u&gt;&lt;span style='font-size:11.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;'&gt;If&#xD;&#xA;you have ever held a Broker license in Pennsylvania please STOP. &lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;b&gt;&lt;u&gt;&lt;span&#xD;&#xA;style='font-size:11.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;'&gt;DO NOT CONTINUE.&#xD;&#xA;Contact the Commission for instructions.&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&#xD;&#xA;&#xD;&#xA;&lt;p class=MsoNormal&gt;&lt;span style='font-size:11.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;'&gt;Please&#xD;&#xA;complete all items in the checklist on the left-side of the screen.&lt;span&#xD;&#xA;style='color:red'&gt;&amp;nbsp; &lt;/span&gt;Each item in the checklist must be completed&#xD;&#xA;before you can pay the required fee and submit the application.&amp;nbsp; Upon&#xD;&#xA;completing each checklist item a checkmark will appear in the box next to the&#xD;&#xA;item&lt;/span&gt;&lt;/p&gt;&#xD;&#xA;&#xD;&#xA;&lt;p class=MsoNormal&gt;&lt;b&gt;&lt;u&gt;&lt;span style='font-size:11.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;'&gt;Examination:&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;span&#xD;&#xA;style='font-size:11.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;'&gt;&amp;nbsp; A passing&#xD;&#xA;score on the Pennsylvania (state) portion and the national (general) portion of&#xD;&#xA;the broker examination within the past three years OR in lieu of the national&#xD;&#xA;(general) portion of the examination have been licensed&amp;nbsp;and active as a&#xD;&#xA;real estate broker in another jurisdiction within the past five&#xD;&#xA;years. &lt;/span&gt;&lt;/p&gt;&#xD;&#xA;&#xD;&#xA;&lt;p class=MsoNormal&gt;&lt;b&gt;&lt;span style='font-size:11.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;'&gt;*Licensees&#xD;&#xA;who wish to use and advertise a nickname for their first name shall include the&#xD;&#xA;nickname on their licensure applications or biennial renewal applications. &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&#xD;&#xA;&lt;br/&gt;&lt;br/&gt;" PageRow="0" PageColumn="0" Rowspan="1" Colspan="1" CID="AppInstructionsLabel" System="false" NodeLevel="License Type">
                          <Attributes>
                            <Attribute Name="class" Value="label" />
                          </Attributes>
                        </Control>
                        <Control Visible="true" Type="com.sac.eLicense.Controls.HyperLink" ReadOnly="false" Required="false" Value="Next..." PageRow="2" PageColumn="0" Rowspan="1" Colspan="1" CID="hyperlink_next" System="false" NodeLevel="License Type">
                          <Attributes>
                            <Attribute Name="href" Value="Demographics.aspx" />
                          </Attributes>
                        </Control>
                      </Controls>
                    </LicenseType>
                     .
                     .
                     .
                  </LicenseTypes>
                </Profession>
              </Professions>
            </Agency>
          </Agencies>
        </Process>
      </Processes>
    </Client>
  </Clients>
</Module>

我需要在Process下面添加以下节点,其中Name =“APP”对于它下面显示的每种许可类型。它必须是Controls节点的子节点;即其他控制节点的兄弟。

                        <Control Visible="true" Type="com.sac.eLicense.Controls.Label" ReadOnly="false" Required="false" Value="&lt;body&gt;&#xD;&#xA;&#xD;&#xA;&lt;p&gt;&lt;font face=&quot;Arial&quot; size=&quot;3&quot;&gt;&lt;b&gt;TEST LABEL&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;&#xD;&#xA;&lt;p class=&quot;MsoNormal&quot; align=&quot;center&quot; style=&quot;text-align:center&quot;&gt;&lt;b&gt;&lt;font face=&quot;Arial&quot; size=&quot;2&quot;&gt;&lt;span style=&quot;mso-bidi-font-size: 12.0pt&quot;&gt;&amp;nbsp;&lt;o:p&gt;&#xD;&#xA;&lt;/o:p&gt;&#xD;&#xA;&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;&#xD;&#xA;&lt;font face=&quot;Arial&quot; size=&quot;2&quot;&gt;&#xD;&#xA;&lt;span style=&quot;mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Times New Roman; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA&quot;&gt;&lt;br clear=&quot;all&quot; style=&quot;mso-special-character:line-break;&#xD;&#xA;page-break-before:always&quot;&gt;&#xD;&#xA;&lt;/span&gt;&#xD;&#xA;&#xD;&#xA;&lt;/font&gt;&#xD;&#xA;&lt;/body&gt;" PageRow="0" PageColumn="0" Rowspan="1" Colspan="1" CID="AppInstructionsLabel" System="false" NodeLevel="License Type">
                          <Attributes>
                            <Attribute Name="class" Value="label" />
                          </Attributes>
                        </Control>

我已经使用以下代码导航到进程名称=“APP”,但是从那里我无法再根据上面的示例选择Controls节点并添加子节点。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\Users\c-jerbaker\Desktop\ProcessStart.xml");
            XmlNodeList processNodes = doc.GetElementsByTagName("Process");
            foreach (XmlNode processNode in processNodes)
            {
                XmlAttributeCollection attributeCollection = processNode.Attributes;
                foreach (XmlAttribute attribute in attributeCollection)
                {
                    if (attribute.Value == "APP")
                    {
                        XmlNodeList controlNodes = processNode.SelectNodes("Controls");
                        foreach (XmlNode controlNode in controlNodes)
                        //{

                        //    XmlNode node = doc.CreateElement("Control");
                        //    XmlAttribute attribute2 = doc.CreateAttribute("Visible");
                        //    attribute2.Value = "true";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("Type");
                        //    attribute2.Value = "com.sac.eLicense.Controls.Label";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("ReadOnly");
                        //    attribute2.Value = "false";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("Required");
                        //    attribute2.Value = "false";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("Value");
                        //    attribute2.Value = "&lt;body&gt;&#xD;&#xA;&#xD;&#xA;&lt;p&gt;&lt;font face=&quot;Arial&quot; size=&quot;3&quot;&gt;&lt;b&gt;TEST LABEL&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;&#xD;&#xA;&lt;p class=&quot;MsoNormal&quot; align=&quot;center&quot; style=&quot;text-align:center&quot;&gt;&lt;b&gt;&lt;font face=&quot;Arial&quot; size=&quot;2&quot;&gt;&lt;span style=&quot;mso-bidi-font-size: 12.0pt&quot;&gt;&amp;nbsp;&lt;o:p&gt;&#xD;&#xA;&lt;/o:p&gt;&#xD;&#xA;&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;&#xD;&#xA;&lt;font face=&quot;Arial&quot; size=&quot;2&quot;&gt;&#xD;&#xA;&lt;span style=&quot;mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Times New Roman; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA&quot;&gt;&lt;br clear=&quot;all&quot; style=&quot;mso-special-character:line-break;&#xD;&#xA;page-break-before:always&quot;&gt;&#xD;&#xA;&lt;/span&gt;&#xD;&#xA;&#xD;&#xA;&lt;/font&gt;&#xD;&#xA;&lt;/body&gt;";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("PageRow");
                        //    attribute2.Value = "0";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("PageColumn");
                        //    attribute2.Value = "0";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("Rowspan");
                        //    attribute2.Value = "2";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("Colspan");
                        //    attribute2.Value = "1";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("CID");
                        //    attribute2.Value = "AppInstructionsLabel";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("System");
                        //    attribute2.Value = "false";
                        //    node.Attributes.Append(attribute2);
                        //    attribute2 = doc.CreateAttribute("NodeLevel");
                        //    attribute2.Value = "LicenseType";
                        //    node.Attributes.Append(attribute2);

                        //    controlNode.AppendChild(node);


                        //    doc.Save("ProcessStart.xml");
                        //    Console.ReadLine();

                        //}

                    }

                }
            }
        }
    }
}

评论部分是我尝试构建要添加的节点的地方,但是无法访问那些控制台节点我甚至无法开始测试我是否正在执行下一部分。我一直在网上搜索试图找出更多关于XML导航和创建的信息,但此时我很难过。如果有人可以指出我正确的方向或向我解释我不理解XML我会很感激。

感谢。

0 个答案:

没有答案