编辑cd​​ata中的元素

时间:2015-03-25 21:09:29

标签: c# xml cdata

我试图编辑以下xml中的元素,但我不确定如何处理cdata代码......我要编辑的元素是FileID

<Import>
<MethodParameters>
    <messageXml><![CDATA[
            <Message>
              <ImportRequest Entity="Users">
                <File Name="XXXXXX" Type="Xml" FileID="TestM-00" TotalPartialFiles="1" PartialSequence="1" Md5="">
                <![CDATA[
                    <ImportUserList>
                        <ImportUser UserName="TestM01" FirstName="TestM01" MiddleName="TestM01" LastName="TestM01"  Active="true" Email="TestM01@gmail.com"  ExternalId="TestM01"/> 
                        <ImportUser UserName="TestM02" FirstName="TestM02" MiddleName="TestM02" LastName="TestM02"  Active="true" Email="TestM02@gmail.com"  ExternalId="TestM02"/> 
                    </ImportUserList>
                    ]]]]><![CDATA[>
                </File >
              </ImportRequest>
            </Message>
        ]]></messageXml>
  </MethodParameters>
</Import>

我在C#中有这样的东西,但我不知道如何更新Users.xml文件,因为我知道AdminNode在第二个文档中

var doc = new XmlDocument();            
doc.Load("E:\\Users.xml");

XmlNode node = doc.SelectSingleNode("/Import/MethodParameters/messageXml").FirstChild;

string newID = Guid.NewGuid().ToString();

XmlNodeList xmlRootNode = doc.GetElementsByTagName("messageXml");
String Sxml = xmlRootNode.Item(0).InnerText;
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(Sxml);

XmlNodeList AdminNode = xdoc.GetElementsByTagName("Message");
AdminNode.Item(0).SelectSingleNode("//ImportRequest/File/@FileID").Value = newID;

doc.Save("E:\\Users.xml");

1 个答案:

答案 0 :(得分:1)

以下代码可以解决这个问题:

    string xml = @"<Import>
                    <MethodParameters>
                        <messageXml><![CDATA[
                                <Message>
                                  <ImportRequest Entity=""Users"">
                                    <File Name=""XXXXXX"" Type=""Xml"" FileID=""TestM-00"" TotalPartialFiles=""1"" PartialSequence=""1"" Md5="""">
                                    <![CDATA[
                                        <ImportUserList>
                                            <ImportUser UserName=""TestM01"" FirstName=""TestM01"" MiddleName=""TestM01"" LastName=""TestM01""  Active=""true"" Email=""TestM01@gmail.com""  ExternalId=""TestM01""/> 
                                            <ImportUser UserName=""TestM02"" FirstName=""TestM02"" MiddleName=""TestM02"" LastName=""TestM02""  Active=""true"" Email=""TestM02@gmail.com""  ExternalId=""TestM02""/> 
                                        </ImportUserList>
                                        ]]]]><![CDATA[>
                                    </File>
                                  </ImportRequest>
                                </Message>
                            ]]></messageXml>
                      </MethodParameters>
                    </Import>";
    var newID = Guid.NewGuid().ToString();
    var xDoc = XDocument.Parse(xml);
    var messageNode = xDoc.XPathSelectElement(".//messageXml");
    var innerDoc = XDocument.Parse(messageNode.Value);
    innerDoc.XPathSelectElement(".//ImportRequest/File").Attribute("FileID").Value = newID;
    messageNode.ReplaceAll(new XCData(innerDoc.ToString()));

现在很脏,但至少会产生预期的输出:

<Import>
  <MethodParameters>
    <messageXml><![CDATA[<Message>
      <ImportRequest Entity="Users">
        <File Name="XXXXXX" Type="Xml" FileID="609bead3-3295-4d68-a07f-1ee47db756fd" TotalPartialFiles="1" PartialSequence="1" Md5="">
            <![CDATA[
                <ImportUserList>
                    <ImportUser UserName="TestM01" FirstName="TestM01" MiddleName="TestM01" LastName="TestM01"  Active="true" Email="TestM01@gmail.com"  ExternalId="TestM01"/> 
                    <ImportUser UserName="TestM02" FirstName="TestM02" MiddleName="TestM02" LastName="TestM02"  Active="true" Email="TestM02@gmail.com"  ExternalId="TestM02"/> 
                </ImportUserList>
            ]]]]><![CDATA[>
        </File>
      </ImportRequest>
    </Message>]]></messageXml>
  </MethodParameters>
</Import>