2
    WebRequest req = HttpWebRequest.Create("http://example.com/example");
    WebResponse res = req.GetResponse();
    StreamReader sr = new StreamReader(res.GetResponseStream());
    string str = sr.ReadToEnd();
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(str);
    XmlTextWriter writer = new XmlTextWriter("D://myGameSite//myGames.xml", null);
    writer.Formatting = Formatting.Indented;
    doc.Save(writer);**strong text**
    Response.Write(doc);

now this code is working perfectly I mean it saves the XML file in the root directory of the myGameSite with the file named myGames, but when I try to display this XML file in browser as you can see in the code, it just plainly displays this

    System.Xml.XmlDocument

I want to display this XML file in my browser along with the tags, and the version of my asp.net and .netframework is 2.0 so please I can't use LINQ :(

Yahia
  • 69,653
  • 9
  • 115
  • 144

8 Answers8

6

This will do ;)

Response.Write("<pre><code>" + Server.HtmlEncode(doc.InnerXml) + "</code></pre>");

EDIT

Or you can display the xml in a textarea

<asp:TextBox ID="txtXml" runat="server" TextMode="MultiLine" Height="500px" Width="600px" />

Code behind

// This will preserve indentation 
    txtXml.Text = doc.InnerXml;
Sandeep Kumar M
  • 3,841
  • 3
  • 38
  • 59
1

For those who uses C# as back-end and angular as front-end mcv framework the following code will work (kind of a workaround).

[BACK-END]
using Newtonsoft.Json;
using System.Xml;
//-----
public ActionResult QueryGenerator(string naturalQuery)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(path_to_xml);
    string jsonText = JsonConvert.SerializeXmlNode(doc);

    return Json( new{xmlString = jsonText}, JsonRequestBehavior.AllowGet);
}

[JAVASCRIPT]
$http({
    method: 'GET',
    url: baseUrl + 'QueryGenerator',
    params: params
}).
  success(function (data, status, config) {
      $scope.xmlList = JSON.parse(data.xmlString);
  }).
  error(function (data, status, config) {
     alert('error');
  });

[HTML]
<xmp ng-repeat="node in xmlList track by $index">
    <Discource>
        <Token>
            <Term>{{node.Term}}</Term>
            <PosTag>{{node.PosTag}}</PosTag>
            <Restriction>{{node.Restriction}}</Restriction>
            <Synonyms>{{node.Synonyms}}</Synonyms>
        </Token>
    </Discource>
</xmp>

enter image description here

Charitha Goonewardena
  • 4,418
  • 2
  • 36
  • 38
1

in your last row use

Response.Write(doc.InnerXml);
Timur Sadykov
  • 10,859
  • 7
  • 32
  • 45
  • its displaying it, but without any tags, below is the example data that is being shown in the browser 100510a0AF0000006oO0lMAETC15740truefalse220000.00 these are different nodes data – Muhammad Mamoor Khan Feb 03 '12 at 06:16
1

Since you have the xml available, as str, just write that:

Response.Write(str);

note that this won't include any formatting changes - if you need that, you'll either need to write the xml again (to the output), or load the file you just wrote. You'll also want to change the response-type to "text/xml". Unrelated, but note you should also close the writer, ideally with using:

var settings = new XmlWriterSettings();
settings.Indent = true;
using(var writer = XmlWriter.Create(path, settings)) {
  doc.Save(writer);
}

You could also repeat the Save:

using(var writer = XmlWriter.Create(Response.Output, settings)) {
  doc.Save(writer);
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • yeah I did that too, but that was too just displaying nodes data without any indentation – Muhammad Mamoor Khan Feb 03 '12 at 06:24
  • @mamoorkhan yes, but the only place you wrote it with indentation was to the file... so you will either need to use `File.ReadAllText`, or **separately**, repeat the `doc.Save`, but to the output stream (see edit) – Marc Gravell Feb 03 '12 at 06:31
1

google "pretty print xml c#"; e.g. http://www.expertcore.org/viewtopic.php?t=1101

you'll find working code, I'm sure. Here's one example: Format XML String to Print Friendly XML String

pass doc.OuterXml to that function that you find; it'll spit out indented xml

set this xml to some multiline textbox in readonly more or some label; unless it's textbox/textarea, you'll have to html-encode it (e.g. for a label):

this.someLabel.Text = this.Server.HtmlEncode(prettyPrintedXml)

Community
  • 1
  • 1
0

Try the following code:

    public static string IndentedPrint(XmlDocument doc)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.Unicode))
            {
                xmlTextWriter.Formatting = Formatting.Indented;

                doc.WriteContentTo(xmlTextWriter);
                xmlTextWriter.Flush();
                memoryStream.Flush();

                memoryStream.Position = 0;

                using (StreamReader sr = new StreamReader(memoryStream))
                {
                    return sr.ReadToEnd();
                }
            }
        }
    }
Sergey Malyutin
  • 1,484
  • 2
  • 20
  • 44
0

Just use the XMP tag... it will indent and make your xml almost look pretty!

<XMP>
  <po>
    <BeginningSegment_BEG>
      <TransactionSetPurposeCode_01>00</TransactionSetPurposeCode_01>
      <PurchaseOrderTypeCode_02>NE</PurchaseOrderTypeCode_02>
      <PurchaseOrderNumber_03>PO01183082</PurchaseOrderNumber_03>
      <Date_05>2016-12-14</Date_05>
    </BeginningSegment_BEG>
  </po>
</XMP>

and it will look like this in your browser: xmp output

It worked for me in Chrome and IE

D. Kermott
  • 1,613
  • 17
  • 24
0

Use XmlDatasource and Data Controls (GridView/Repeator) to show XML document data.

<?xml version="1.0" encoding="iso-8859-1"?>
 <root>
   <foo>
     <bar>One</bar>
   </foo>
   <foo>
     <bar>Two</bar>
   </foo>
 </root>

Markup:

<asp:XmlDataSource
    runat="server"
    id="XmlDataSource1"
    XPath="root/foo"
    DataFile="file.xml" />

<asp:Repeater ID="Repeater1"
    runat="server"
    DataSourceID="XmlDataSource1">
    <ItemTemplate>
       <h2>Bar :<%#XPath("bar")%> </h2>
    </ItemTemplate>
</asp:Repeater>
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186