c# - Read xml file just as it is -


i have xml file has following

all i'm trying display text in multi line textbox in file. i've found code on microsoft site , altered work me i'm still not quite there.

<employees>   <employee>     <name>davolio, nancy</name>     <title>sales representative</title>     <birthday>12/08/1948</birthday>     <hiredate>05/01/1992</hiredate>   </employee>   <employee>     <name>fuller, andrew</name>     <title>vice president, sales</title>     <birthday>02/19/1952</birthday>     <hiredate>08/14/1992</hiredate>   </employee>   <employee>     <name>leverling, janet</name>     <title>sales representative</title>     <birthday>08/30/1963</birthday>     <hiredate>04/01/1992</hiredate>   </employee> 

code:

xmltextreader reader = new xmltextreader("employees.xml");  string contents = ""; while (reader.read()) {     reader.movetocontent();     if (reader.nodetype == system.xml.xmlnodetype.element)         contents += "<" + reader.name + ">\n ";     if (reader.nodetype == system.xml.xmlnodetype.text)         contents += reader.value + "</" + reader.name+ ">\n";  }  //console.write(contents);  txtstats.text = "file creation time = " + file.getcreationtime(server.mappath("../xmlfiles/employees.xml")).tostring()     + "\n" + "file last access time = " + file.getlastaccesstime(server.mappath("../xmlfiles/employees.xml")).tostring()     + "\n" + "file last write time = " + file.getlastwritetime(server.mappath("../xmlfiles/employees.xml")).tostring()     + "\n"     + "\n"     + contents.tostring(); 

this gets me following.

<employees>  <employee>  <name>  davolio, nancy</> <title>  sales representative</> <birthday>  12/08/1948</> <hiredate>  05/01/1992</> <employee>  <name>  fuller, andrew</> <title>  vice president, sales</> <birthday>  02/19/1952</> <hiredate>  08/14/1992</> <employee>  <name>  leverling, janet</> <title>  sales representative</> <birthday>  08/30/1963</> <hiredate>  04/01/1992</> 

if there better way of doing i'm happy hear of alternatives.

if want display file is, don't need parse xml. can use file.readalltext:

textbox1.text = file.readalltext("employees.xml"); 

alternatively, if wanted format it, easy way run through xdocument, this:

textbox1.text = xdocument.load("employees.xml").tostring(); 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -