简体   繁体   中英

C# Display XML as Web API web service

I have an external xml file with the following format that I want to return the same format in a web service

<properties>
  <ID>1</ID>
  <configName>name</configName>
  <config>test</config>
</properties>

C# code:

public String Index()
    {
        var configLoc = WebConfigurationManager.AppSettings["configLocation"];
        var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), configLoc);
        //string myXML = System.IO.File.ReadAllText(path);
        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        return doc.InnerXml;
    }

The problem is that I am returning the XML as a string with just a general 'string' node.

I think my problem relates to InnerXML ,but I am not sure how to respect the XML nodes. How can I get the web format to look the same as the XML file?

I was able to figure this one out....

public HttpResponseMessage Index()
    {
        var configLoc = WebConfigurationManager.AppSettings["configLocation"];
        var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), configLoc);
        var response = new HttpResponseMessage();
        response.Content = new StringContent(File.ReadAllText(path));
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
        return response;
    }

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