0

I can read and format an XML file and show in in View.

But I cant find any reference how can I just pick the XML file from a URL and display it as it is (an XML) without formatting. I just want to view the file from my site.

I just need to view the schema. For some reason I cant view the file in my home computer and only my host IP cant access the file.

Im looking for something like this:

public ActionResult ViewXMLFile()
{
    Response.ContentType = "text/xml";
    XmlDocument doc = new XmlDocument();
    doc.Load(xmlPath_here);

    [then return a view displaying the XML as is]
}
SKS
  • 81
  • 8
  • Can't you just save the xml as a string and put it in a web element? Can you provide the code so we can have a better understanding. – Josh Adams Jan 11 '18 at 23:52
  • You have to make sure your web.config can output xml. https://stackoverflow.com/questions/9666873/how-to-add-a-xml-in-web-config – Iskandar Reza Jan 11 '18 at 23:53
  • check my edit.. – SKS Jan 11 '18 at 23:57
  • Very unclear what you are asking. Clearly you've searched https://www.bing.com/search?q=c%23+mvc+return+xml+file and know how to return XML from action... "For some reason I cant view the file in my home computer and only my host IP cant access the file" remark feels like you actually interested in much broader question "how to access my machine over internet" which would be off-topic on SO... I'd recommend [edit] the post and at least clarify what you expect "display it as-is" (presumably just as plain text?) – Alexei Levenkov Jan 12 '18 at 02:19
  • @Alexei Levenkov. The XML file is provided by clients and from their XML File, we will transfer the data to DB, Each client have different XML schema. So I need to check the XML Schema before creating a script that will process their xml to DB. So the XML will be fetch by my script that is in my host server. "As is" means retrieving it as as XML, as if you double click the xml file from your desktop and viewing it in your browser. – SKS Jan 12 '18 at 07:14
  • I don't think your comment makes any more clear (and it makes it even more confusing where and what type of code you try to run). In any case if you decide to [edit] post or ask new question please stick to *one* concrete issue per post. Good luck. – Alexei Levenkov Jan 12 '18 at 15:45

1 Answers1

0

You need to get the XML from the URL before you can display it.

XmlDocument document = new XmlDocument();
document.Load(urlString);
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/root/order"); 


foreach (XmlNode node in nodes)
{
    string idStr = node["order_id"].InnerText;
    string dateStr = node["order_date"].InnerText;

    XmlElement root2 = doc.DocumentElement;
    XmlNodeList nodes2 = root.SelectNodes("/root/order/item"); 
     foreach (XmlNode nodex in nodes)
    {
          string sku = nodex["item_sku"].InnerText;
    }
}

Check out here: http://www.softwarepassion.com/reading-xml-from-the-web-using-c/

then just render it in the view.

Josh Adams
  • 2,113
  • 2
  • 13
  • 25
  • `SelectNodes("/root/order")` This is not possible since I dont know the nodes of the XML file. Thats why I want to view it to check the schema – SKS Jan 12 '18 at 00:50
  • my script doesnt need to "know" the contents of the XML. It only need to display whatever the contents of it as it is. – SKS Jan 12 '18 at 00:57
  • Sorry @SKS i misunderstood. I did search microsoft support and found a way you can read the xml https://support.microsoft.com/en-us/help/307643/how-to-read-xml-data-from-a-url-by-using-visual-c Hope that helps some! – Josh Adams Jan 12 '18 at 01:02