Search code examples
c#xmlcdata

Edit an element inside cdata


I'm trying to edit an element in the following xml, but I'm not sure how to deal with the cdata code... the element I want to edit is 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="[email protected]"  ExternalId="TestM01"/> 
                        <ImportUser UserName="TestM02" FirstName="TestM02" MiddleName="TestM02" LastName="TestM02"  Active="true" Email="[email protected]"  ExternalId="TestM02"/> 
                    </ImportUserList>
                    ]]]]><![CDATA[>
                </File >
              </ImportRequest>
            </Message>
        ]]></messageXml>
  </MethodParameters>
</Import>

I have something like this in C#, but I'm not sure how to update the Users.xml file, because I know that AdminNode is in the second document

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");

Solution

  • The following code should do the trick:

        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=""[email protected]""  ExternalId=""TestM01""/> 
                                                <ImportUser UserName=""TestM02"" FirstName=""TestM02"" MiddleName=""TestM02"" LastName=""TestM02""  Active=""true"" Email=""[email protected]""  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()));
    

    It's pretty dirty now, but at least it produces the expected output:

    <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="[email protected]"  ExternalId="TestM01"/> 
                        <ImportUser UserName="TestM02" FirstName="TestM02" MiddleName="TestM02" LastName="TestM02"  Active="true" Email="[email protected]"  ExternalId="TestM02"/> 
                    </ImportUserList>
                ]]]]><![CDATA[>
            </File>
          </ImportRequest>
        </Message>]]></messageXml>
      </MethodParameters>
    </Import>