0
votes

I have my xml file which is in this format:

<?xml version="1.0" encoding="utf-8"?>
<Performance>
  <transaction name="Home">
    <request transaction="Home">
      <URL>http://192.168.0.7:23267/</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>200</RESPONSE-CODE>
    </request>
    <request transaction="Home">
      <URL>http://192.168.0.7:23267/images/banner.JPG</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>200</RESPONSE-CODE>
    </request>
    <request transaction="Home">
      <URL>http://192.168.0.7:23267/favicon.ico</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>404</RESPONSE-CODE>
    </request>
  </transaction>
 <transaction name="Login">
    <request transaction="Login">
      <URL>http://192.168.0.7:23267/Login.aspx</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>200</RESPONSE-CODE>
    </request>
    <request transaction="Login">
      <URL>http://192.168.0.7:23267/images/banner.JPG</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>304</RESPONSE-CODE>
    </request>
    <request transaction="Login">
      <URL>http://192.168.0.7:23267/favicon.ico</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>404</RESPONSE-CODE>
    </request>
    <request transaction="Login">
      <URL>http://192.168.0.7:23267/Login.aspx</URL>
      <METHOD>POST</METHOD>
      <RESPONSE-CODE>302</RESPONSE-CODE>
      <Content-Type>application\x-www-url-encoded</Content-Type>
      <Request_Body>__VIEWSTATE=%2FwEPDwUKLTIyNjM0Mjg2OGRki4v1QSB8PsGDDRUcxLe8nvnfdRE%3D&amp;__VIEWSTATEGENERATOR=C2EE9ABB&amp;__EVENTVALIDATION=%2FwEWBAL985OpBAKNo6vIDwLIoPPsBAK63N3jDihcqbsKpbaFs%2FjuUd7THytQsuqK&amp;ctl00%24ContentPlaceHolder1%24username=aadi&amp;ctl00%24ContentPlaceHolder1%24password=aadi1234&amp;ctl00%24ContentPlaceHolder1%24log=Login</Request_Body>
    </request>
    <request transaction="Login">
      <URL>http://192.168.0.7:23267/securepage/SecurePage.aspx</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>200</RESPONSE-CODE>
    </request>
    <request transaction="Login">
      <URL>http://192.168.0.7:23267/images/banner.JPG</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>304</RESPONSE-CODE>
    </request>
  </transaction>
  <transaction name="Logout">
    <request transaction="Logout">
      <URL>http://192.168.0.7:23267/securepage/SecurePage.aspx</URL>
      <METHOD>POST</METHOD>
      <RESPONSE-CODE>302</RESPONSE-CODE>
      <Content-Type>application\x-www-url-encoded</Content-Type>
      <Request_Body>__VIEWSTATE=%2FwEPDwULLTIwMDIzMDY0MjdkZLJV17BO2R5dj5TTwq3Xxpx8sGBn&amp;__VIEWSTATEGENERATOR=0900BECB&amp;__EVENTVALIDATION=%2FwEWAgKd%2BtjcCAKA4sljkvXRoUoTS7vStAm0LmWvI9N4A0w%3D&amp;ctl00%24ContentPlaceHolder1%24Button1=Logout</Request_Body>
    </request>
    <request transaction="Logout">
      <URL>http://192.168.0.7:23267/securepage/Login.aspx</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>302</RESPONSE-CODE>
    </request>
    <request transaction="Logout">
      <URL>http://192.168.0.7:23267/Login.aspx?ReturnUrl=%2fsecurepage%2fLogin.aspx</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>200</RESPONSE-CODE>
    </request>
    <request transaction="Logout">
      <URL>http://192.168.0.7:23267/images/banner.JPG</URL>
      <METHOD>GET</METHOD>
      <RESPONSE-CODE>304</RESPONSE-CODE>
    </request>
  </transaction>
 </Performance>

I want to parse this file and read each transaction tag attribute as a root of treeview i.e. for example Home will be a root node of treeview and then URL tag value as its child node and same thing for all transaction and URL tags means Login as root node than respective URL tag value as its child node.

3

3 Answers

1
votes

You could create the scheme of the xml and than the cs-class for it with command line calls:

xsd xml.xml
xsd xml.xsd /c

that will produce xml.cs file. you could deserialize it with common xml serializer.

    XmlSerializer ser = new XmlSerializer(typeof(Performance));
    var t = ser.Deserialize(new XmlTextReader(@"C:\temp\xml.xml"));

After deserialization you do have normal c# object with tree structure:

enter image description here

If you have problems creating cs file, here it is:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Performance {

    private PerformanceTransaction[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("transaction", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PerformanceTransaction[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class PerformanceTransaction {

    private PerformanceTransactionRequest[] requestField;

    private string nameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("request", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PerformanceTransactionRequest[] request {
        get {
            return this.requestField;
        }
        set {
            this.requestField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class PerformanceTransactionRequest {

    private string uRLField;

    private string mETHODField;

    private string rESPONSECODEField;

    private string contentTypeField;

    private string request_BodyField;

    private string transactionField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string URL {
        get {
            return this.uRLField;
        }
        set {
            this.uRLField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string METHOD {
        get {
            return this.mETHODField;
        }
        set {
            this.mETHODField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("RESPONSE-CODE", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string RESPONSECODE {
        get {
            return this.rESPONSECODEField;
        }
        set {
            this.rESPONSECODEField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Content-Type", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string ContentType {
        get {
            return this.contentTypeField;
        }
        set {
            this.contentTypeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Request_Body {
        get {
            return this.request_BodyField;
        }
        set {
            this.request_BodyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string transaction {
        get {
            return this.transactionField;
        }
        set {
            this.transactionField = value;
        }
    }
}
0
votes
XmlDocument xml = new XmlDocument();
            xml.Load("E://session1.xml"); // suppose that myXmlString contains "<Names>...</Names>"
            string roots = xml.DocumentElement.Name;
            treeView1.Nodes.Add(roots);
            TreeNode ParentNode = new TreeNode();
            XmlElement root = xml.DocumentElement;

            if (root.HasAttribute("name"))
            {
                {
                    String name = root.GetAttribute("name");
                    ParentNode.Text = name;
                    treeView1.Nodes.Clear();
                    treeView1.Nodes.Add(ParentNode);
                }
            }
            //while(xml.)
            XmlNodeList xnList = xml.DocumentElement.SelectNodes("/transaction/request");
            foreach (XmlNode xn in xnList)
            {

                string url = xn["URL"].InnerText;
                ParentNode.Nodes.Add(url);
                treeView1.ExpandAll();
            }

Above mentioned code help ne to generate a treeview with first tag and tag value but it does not work for second and third transaction tags.is it the correct way to proceed or else i have to try it by some other way.

0
votes

Use XML Linq. Start with this code. You can use your code on results.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("transaction").Select(x => new
            {
                name = x.Attribute("name").Value,
                request = x.Elements("request").Select(y => new {
                    transaction = y.Attribute("transaction").Value,
                    URLs = y.Element("URL").Value
                }).ToList()
            }).ToList();
        }
    }
}
​

or use this code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
            const string FILENAME = @"c:\temp\test.xml";
        public Form1()
        {
            InitializeComponent();

            XDocument doc = XDocument.Load(FILENAME);
            TreeNode root = new TreeNode();
            treeView1.Nodes.Clear();

            foreach (XElement transaction in doc.Descendants("transaction"))
            {
                string name = transaction.Attribute("name").Value;
                TreeNode transactionNode = treeView1.Nodes.Add(name);

                foreach (XElement request in transaction.Elements("request"))
                {
                    string transactionName = request.Attribute("transaction").Value;
                    string URLs = request.Element("URL").Value;
                    string node = string.Format("Transaction : {0}, URL : {1}", transactionName, URLs);
                    transactionNode.Nodes.Add(node);
                }

            }

            treeView1.ExpandAll();
        }
    }
}
​