-1

consider my source file looks like this.

        <Content xmlns="uuid:4522eb85-0a47-45f9-8e2b-1x82c78xx920">
            <first>Hello World.This is Fisrt field</first>
            <second>Hello World.This is second field</second>
   </Content>

I want to write a code, which read this xml document from a location and display it as string.

  say name of the xml file is helloworld.xml.
  Location: D:\abcd\cdef\all\helloworld.xml.

I have tried the following, but i was unable to do it.

            XmlDocument contentxml = new XmlDocument();
            contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
            Response.Write("<BR>" + contentxml.ToString());

Response.write is displaying nothing. Correct me if i missed any thing. Its not creating any component and error is coming.

I have also tried this,

            XmlDocument contentxml = new XmlDocument();
            try
            {
                 contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
             }
            catch (XmlException exp)
            {
                Console.WriteLine(exp.Message);
            }
            StringWriter sw = new StringWriter();
            XmlTextWriter xw = new XmlTextWriter(sw);
            contentxml.WriteTo(xw);
            Response.Write("<BR>" + sw.ToString());

But i did not find the any output.

I want to read a XML file from a location and display it as it is as string.

Can anyone help on this.

Thank you, Muzimil.

Patan
  • 17,073
  • 36
  • 124
  • 198

6 Answers6

5

You need the OuterXml property:

Response.Write("<BR>" + contentxml.OuterXml);

Also you are loading a file not xml so use

  contentxml.Load(@"D:\abcd\cdef\all\helloworld.xml");

instead of

  contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

Do you really have to deserialize the XML at all? Why not just read it as a text file? Something like..

String text = File.ReadAllText(@"D:\abcd\cdef\all\helloworld.xml");
Response.Write(text);

With appropriate error handling obviously..

Nick Thomson
  • 211
  • 3
  • 10
1

I would try using the XDocument class:

//load the document from file
var doc = XDocument.Load("..."); //== path to the file

//write the xml to the screen
Response.Write(doc.ToString());

If you want to use an XmlDocument instead, you would want to use Load instead LoadXml.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
0
String text = File.ReadAllText(Server.MapPath("~/App_Data/sample.xml"));
txtData.Text = text;
Flexo
  • 87,323
  • 22
  • 191
  • 272
0

If you want to simply write a file to the output, you can do Response.WriteFile.

Rob Rodi
  • 3,476
  • 20
  • 19
0

try this

XmlTextReader reader = new XmlTextReader (@"D:\abcd\cdef\all\helloworld.xml");
while (reader.Read()) 
{
    Console.WriteLine(reader.Name);
}
Console.ReadLine();
Rizwan Mumtaz
  • 3,875
  • 2
  • 30
  • 31