Question

I have the following code and it does what I want it to do. It gets XML data from a web call and puts it in an object.

string url = ConfigurationManager.AppSettings[CONFIGURATION_KEY_XE_COM_URL];

        var serialiser = new XmlSerializer(typeof(xedatafeed));

        var settings = new XmlReaderSettings {XmlResolver = null, DtdProcessing = DtdProcessing.Parse};

        var reader = XmlReader.Create(url, settings);

        var feed = (xedatafeed) serialiser.Deserialize(reader);

        return feed;

The issue is that I want to get a string representation of this data as well and doing feed.ToString() almost gives me all the data but not everything. There are two things missing

  1. The <?xml version="1.0" encoding="ISO-8859-1"?> declaration
  2. The DTD declaration that came along with the response.

I'd like these two things as well. I had to exclude the DTD declaration as it was not required for deserialisation into an object and it caused an error when I didn't exclude it. What's the cleanest way to get the full response given the above code?

Was it helpful?

Solution

In my experience, it eaiser to either add the declaration to the string after you have the result from the webservice, or declare it it programatically.

Method 1 - Add XML declaration after reciept of data

string xmlStr = string.format(@" &lt; ?xml version=\"1.0\" encoding=\"ISO-8859-1\"? &gt; {0}", WebServiceStringResult)

Then you can create a new XML Document object from the concatenated string with :

YourXmlDocumentObject.ReadXML(xmlStr) 
  1. And then loop over or xpath out the desired values. or
  2. Cast / Manipulate the YourXmlDocumentObject as required.

Method 2 - Programmatically add the XML/DTD declarations after reciept of data

In your new XmlDocument object, set the Properties to match your XML version and DTD information.

Finally, just add your webservice result string with:

YourXmlDocumentObject.ReadXML(WebServiceStringResult);

Note: Soap 1.1 and 1.2 will fall over with the declaration as part of an embedded request. ie the declaration is inside your one of your web methods.

~ ^i'm still learning to use this poxy editor, sorry ~

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top