4

I have an object and would like to convert it into XML and show it on page in its raw format.

Desired output

<Response>
 <ResponseCode>100</ResponseCode>
 <ResponseDescription>Test</ResponseDescription>
</Reponse>

Code:

public class Response
{
    public Response(){}

    public string ResponseCode { get; set; }        
    public string ResponseDescription { get; set; }
}

Page_Load()
{
 Response obj = new Response();
 obj.ResponseCode = "100";
 obj.ResponseDescription = "test";

 string xmlString;
 XmlSerializer serializer = new XmlSerializer(typeof(Response));
 XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
 // exclude xsi and xsd namespaces by adding the following:
 ns.Add(string.Empty, string.Empty);

 using (StringWriter textWriter = new StringWriter())
  {
   using (XmlWriter xmlWriter = XmlWriter.Create(textWriter))
    {
      serializer.Serialize(xmlWriter, obj, ns);
    }
   xmlString = textWriter.ToString(); 
  }    
  Response.Write(xmlString);
}

The result is just like this.

100 test

What should I do to get the desired output.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
sukesh
  • 2,379
  • 12
  • 56
  • 111
  • 1
    Mmm.. May be you looking at response with some tool ? May be this tool already present just a _value_ of XML string ? Try to search for _raw_ response (response as it) – Alex F Mar 01 '16 at 14:32
  • 1
    I'm guessing the browser simply ignores the xml tags. try `Response.Write (Server.HTMLEncode(xmlString));` instead. [HTMLEncode](https://msdn.microsoft.com/en-us/library/ms525347(v=vs.90).aspx) – Zohar Peled Mar 01 '16 at 14:33
  • 2
    The output is a proper XML, but you're seeing the **rendered** text. – rbm Mar 01 '16 at 14:34
  • is Response.Write going to an HTML page? You might need to consider encoding/decoding You seemingly have an object by the name of `Response`, and a static by the name of `Response` ? – Kritner Mar 01 '16 at 14:34
  • @ZoharPeled. That did it :) . But it has xml header (` – sukesh Mar 01 '16 at 14:35

2 Answers2

5

The actual problem is that you're outputting XML as HTML, causing your browser to treat the response as "tag soup" and try to render it as an HTML document.

This hides all tags and their attributes, and only renders the text inside and between tags.

This isn't HTML, it's XML. So the actual solution is to set the proper content-type, indicating that you're actually returning XML, from How do you specify your Content Type in ASP.NET WebForms?:

Response.ContentType = "application/xml";

Additionally, you're asking to omit the XML declaration. From how to create an xml using xml writer without declaration element and MSDN: XmlWriterSettings.OmitXmlDeclaration:

The XML declaration is always written if ConformanceLevel is set to Document, even if OmitXmlDeclaration is set to true.

The XML declaration is never written if ConformanceLevel is set to Fragment

So simply set the settings.ConformanceLevel to ConformanceLevel.Fragment. Note that then you're technically not writing an XML document anymore, but this requirement is common in interoperability.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
3

I'm guessing the browser simply ignores the XML tags. try this instead:

Response.Write (Server.HTMLEncode(xmlString));

Read here about the HTMLEncode method.

the settings.OmitXmlDeclaration = true; should have removed the <?xml version... tag. If that didn't work, you can try this: load the xmlString into an XDocument object and remove it's declaration (based on this answer):

XDocument xdoc = XDocument.Parse(xmlString);
xdoc.Declaration = null;
Response.Write (Server.HTMLEncode(xdoc.ToString()));
Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • When the requirement is to write XML, you generally don't want to "HTML-encode" the output... – CodeCaster Mar 01 '16 at 14:45
  • Can you please see one of my [question](https://stackoverflow.com/questions/55430357/how-to-get-xml-web-service-response-in-c-sharp) regarding the xml response? – Moeez Mar 31 '19 at 05:26