0

This is an unusual situation. We are forced to interface with a 3rd party who requires certain values in the xml response to be wrapped with even if it is just a string value.

Example: <Property name="someName" type="String"><![CDATA[someValue]]></Property>

We are adding these property nodes to the document as follows:

XPathExpression query = xPathNavigator.Compile(xpath);

XPathNavigator node = xPathNavigator.SelectSingleNode(query.Expression, xmlNamespaceManager);

string property = "<Property name='someName' type='String'><![CDATA[someValue]]></Property>";

node.AppendChild(property);

The problem is, the resulting xml looks like this

<Property name="someName" type="String">someValue</Property>

The CDATA keeps getting stripped out.

2 Answers 2

3

You can achieve this by using an XmlWriter to write the data:

private static void WriteProperty(XmlWriter writer, string name, string type, string value)
{
    writer.WriteStartElement("Property");
    writer.WriteAttributeString("name", name);
    writer.WriteAttributeString("type", type);
    writer.WriteCData(value);
    writer.WriteEndElement();

}

// call the method from your code
XPathExpression query = xPathNavigator.Compile(xpath);    
XPathNavigator node = xPathNavigator.SelectSingleNode(query.Expression, xmlNamespaceManager);
using (XmlWriter writer = node.AppendChild())
{
    WriteProperty(writer, "someName", "String", "someValue");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm.. a bit late for beer here (bedtime, in fact). Can you put it in the fridge for the night? ;) (glad it worked out)
0

You might want to check if node.AppendChild().WriteRaw(property) will work, given that you seem to be formatting the XML string manually anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.