0

I am using WEB API to POST the content of a XML file by passing the whole content as a string. However I am getting error and now I am trying to find out how I should pass this XML string? escape slashes, no double quotes, etc....

Here is a small example of my (incorrect) JSON structure:

{"xml":"
<server>
  <networkAdapters>
    <networkAdapter id="3d9c0d9d-d353-4d78-b034-b29f2dd824a0" ipEnabled="true">
      <defaultGateways>
        <defaultGateway address="x.x.x.x" costMetric="20" index="0" />
      </defaultGateways>
      <ipAddresses>
        <ipAddress address="x.x.x.x" subnetMask="255.255.255.192" index="0" />
      </ipAddresses>
      <dnsServers>
        <dnsServer address="x.x.x.x" index="0" />
        <dnsServer address="x.x.x.x" index="1" />
      </dnsServers>
    </networkAdapter>
</server>"}

WEB API code:

        //POST api/RunningValue_Import
        public object PostRunningValue(param_RunningValue inputData)
        {
            try
            {
                #region Retrieve XML string and Create XDocument
                TextReader tr = new StringReader(inputData.xml);
                XDocument doc = XDocument.Load(tr);
                #endregion
...

I am trying to find out what needs to be done before I POST the whole XML content to my WEB API.

Any ideas? Thank you

2
  • Why not just pass the XML directly? Why would you want to embed it into JSON ? Commented Dec 31, 2013 at 13:53
  • stackoverflow.com/questions/10428177/… Commented Dec 31, 2013 at 17:28

2 Answers 2

1

You need to escape double quotes in your xml string like so:

<dnsServer address=\"x.x.x.x\" index=\"0\" />

Also your xml is not well formed, you have not included the closing </networkAdapters> tag.

Sign up to request clarification or add additional context in comments.

2 Comments

well I escaped all double quotes and same problem. Fixed the last tag but that should not matter, I am just passing a long xml STRING; I will only parse it inside the WEB API. Any more suggestions? thank you!
I think I found the solution here: stackoverflow.com/questions/10428177/…
0

Why not just drop the JSON wrapper and send the XML directly?

public HttpResponseMessage Post(XElement xElement)
{

...

}

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.