c# - failure to load file with accented characters using XElement -
i try following code didn't work correctly , idea please .
string file = @"c:\program.xml"; xelement root = xelement.parse(file.readalltext(file).replace("\"", "'"));
an example xml file :
<?xml version="1.0" encoding="utf-8"?> <session xmlns="http://winscp.net/schema/session/1.0" name="test" start="2014-04-04t15:54:09.728z"> <upload> <filename value="d:\ftp\test2.txt" /> <destination value="/in/test2.txt" /> <result success="true" /> </upload> <touch> <filename value="/in/test2.txt" /> <modification value="2014-03-27t12:45:20.000z" /> <result success="false" /> </touch> </session>
i need use xelement further treatment
i think little bit confused
xdocument xdoc=xdocument.load(filepath);
that's need can move withn xml without issue in example
xdoc.root.elements("nameelement").attributes("nameattribute").value
and on.
it's simple :)
here simple example : in vbnet , in c#. assume have simple webpage button event
protected sub btngetvalues_click(sender object, e eventargs) handles btngetvalues.click dim xdoc xdocument = xdocument.load(server.mappath("~/data.xml")) dim ns xnamespace = "http://winscp.net/schema/session/1.0" dim sb new stringbuilder try 'iterate within xmlelement assume code "session" root 'and descendant upload , child , touch childs each el in (from in xdoc.root.descendants(ns + "upload") select a) each subelement in el.descendants response.write("<b>" & subelement.name.tostring & "</b><ul>") if subelement.hasattributes each att in subelement.attributes response.write("<li>" & att.name.tostring & ":" & att.value.tostring & "</li>") next end if response.write("</ul>") next next catch ex exception response.write(ex.message) end try end sub
c# version:
protected void btngetvalues_click(object sender, eventargs e) { xdocument xdoc = xdocument.load(server.mappath("~/data.xml")); xnamespace ns = "http://winscp.net/schema/session/1.0"; stringbuilder sb = new stringbuilder(); try { //iterate within xmlelement assume code "session" root //and descendant upload , child , touch childs foreach (object el_loopvariable in (from in xdoc.root.descendants(ns + "upload")a)) { el = el_loopvariable; foreach (object subelement_loopvariable in el.descendants) { subelement = subelement_loopvariable; response.write("<b>" + subelement.name.tostring + "</b><ul>"); if (subelement.hasattributes) { foreach (object att_loopvariable in subelement.attributes) { att = att_loopvariable; response.write("<li>" + att.name.tostring + ":" + att.value.tostring + "</li>"); } } response.write("</ul>"); } } } catch (exception ex) { response.write(ex.message); } }
this result in page response.write:
{http://winscp.net/schema/session/1.0}filename
- value:d:\ftp\test2.txt
- value:/in/test2.txt
- success:true
Comments
Post a Comment