简体   繁体   中英

XDocument adds carriage (\r\n, ecaspe characters) return when generating final xml string

I need to return IHttpActionResult, Ok(xml).
Variable xml is a string data type. The problem is next...
When the program returns string xml, it adds \\r\\n for a NewLine, string escape characters ().
I want to return a clean xml string.

For example, this string i want to return:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
 <ProxyGlave>
    <Glava ID="530064">
       <DatumIsporuke>1900-01-01T00:00:00</DatumIsporuke>
    </Glava>
    <Glava ID="530065">
       <DatumIsporuke>1900-01-01T00:00:00</DatumIsporuke>
    </Glava>
 </ProxyGlave>

But my program returns this:

     "
     <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n
     <ProxyGlave>\r\n  
        <Glava ID=\"530064\">\r\n    
           <DatumIsporuke>1900-01-01T00:00:00</DatumIsporuke>\r\n
        </Glava>\r\n
        <Glava ID=\"530065\">\r\n    
           <DatumIsporuke>1900-01-01T00:00:00</DatumIsporuke>\r\n
        </Glava>\r\n
     </ProxyGlave>"

Screenshot from Postman tool:

截屏

Code which generates string XML:

XDocument xdoc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
        new XElement("ProxyGlave",
        from gl in glave
        select
            new XElement("Glava", new XAttribute("ID", gl.DokumentiGlaId),
            new XElement("DatumIsporuke", gl.datum_isporuke)))
);
return Ok(xdoc.ToString());

In a Header of the GET request, you need to add Accept key which has value application/xml. With that information in a header, the app knows what type of value it needs to return.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM