0

I try the following code but it didn't work correctly , any idea please .

string file = @"C:\Program.xml"; 
XElement root = XElement.Parse(File.ReadAllText(file).Replace("\"", "'"));

An example for 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 to use XElement for further treatment

1 Answers1

2

i think that you are a little bit confused

Xdocument xdoc=Xdocument.Load(filepath);

that's all you need that you can move withn xml without any issue in example

xdoc.root.elements("nameElement").Attributes("nameAttribute").Value

and so on.

it's really simple :)

here a simple example : in vbnet and then in c#. Assume that you have a simple webpage with a button with its event

 Protected Sub btnGetValues_Click(sender As Object, e As EventArgs) Handles btnGetValues.Click

    Dim xdoc As XDocument = XDocument.Load(Server.MapPath("~/data.xml"))
    Dim ns As XNamespace = "http://winscp.net/schema/session/1.0"
    Dim Sb As New StringBuilder
    Try
        'iterate within xmlelement where assume with this code that "session" is root
        'and descendant are upload and its child and touch with its childs
        For Each el In (From a In xdoc.Root.Descendants(ns + "upload") Select a)
            For Each subelement In el.Descendants
                Response.Write("<b>" & subelement.Name.ToString & "</b><ul>")
                If subelement.HasAttributes Then
                    For 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 As 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 where assume with this code that "session" is root
    //and descendant are upload and its child and touch with its childs
    foreach (object el_loopVariable in (from a 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 is result in page as response.write:

{http://winscp.net/schema/session/1.0}filename

  • value:D:\ftp\test2.TXT
{http://winscp.net/schema/session/1.0}destination
  • value:/in/test2.TXT
{http://winscp.net/schema/session/1.0}result
  • success:true
makemoney2010
  • 1,222
  • 10
  • 11
  • +0: The original document have all nodes in a namespace - you should show example of using namespaces like in http://stackoverflow.com/questions/4985974/xelement-namespaces-how-to – Alexei Levenkov Apr 05 '14 at 18:41