Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

xml part do display array of something, how to read

by the3factory 5/2/2008 9:26:00 PM
Question:

here is xml part example:

                <!-- LogPattern is an array of Log patterns to extract -->
                <LogPattern>MBC-LOG</LogPattern>
                <LogPattern>MFW-LOG</LogPattern>

is there any better way to display this? 

i am wondering how to read this by using XmlDocument.SelectSingleNode(xpath)?

 


Answer1:

Not sure exactly what you mean - if you want a list of all the log pattern nodes (i.e. so is IEnumerable) you could use XPath as follows.

XmlDocument myXmlDoc = new XmlDocument();

XmlDocument.Load("YourXMLDocNameHere.xml"); 

XmlNodeList nodeList = XmlDocument.SelectNode("//LogPatterns/LogPattern"); 

 

Does this help? 


Answer2:

Sorry the XmlDocument variable name would be used....

 
 

XmlDocument myXmlDoc = new XmlDocument();

myXmlDoc.Load("YourXMLDocNameHere.xml"); 

XmlNodeList nodeList = myXmlDoc.SelectNode("//LogPatterns/LogPattern");

 


Answer3:

Hi  check this link

http://aspdotnetcodebook.blogspot.com/2008/04/how-to-populating-dropdownlist-control.html 


Answer4:

Thank you for getting back.

 

  <?xml version="1.0"?>
  <LogPatterns>
    <LogPattern>LOG111</LogPattern>
    <LogPattern>LOG222</LogPattern>
    <LogPattern>LOG333</LogPattern>
  </LogPatterns>
 
  I am binding this xml to treeview on page and to print value when selecting a leaf node in treeview.
 
  Well, I know how to retrieve value using following code in TreeView1_SelectedNodeChanged:
 
    XmlDocument xdd = new XmlDocument();
    xdd.Load(Server.MapPath("log.xml"));
    XmlNode oNode = xdd.SelectSingleNode(TreeView1.SelectedNode.ValuePath+ "[1]"); // the number here is the index of <LogPattern>
 
  HOWEVER, I have no idea how to get the right index number. for example, if user select the second <LogPattern> in treeviw, how do I know if it is second one.

 


Answer5:

Hi justindongqiang ,

You can write some code to do this.

 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TreeNode root = new TreeNode("LogPatterns");
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("XMLFile30.xml"));
            XmlNodeList lst = doc.SelectNodes("//LogPattern");
            for (int i = 0; i < lst.Count; i++)
            {
                TreeNode node = new TreeNode();
                node.Text = "LogPatten";
                node.Value = lst[i].InnerText +","+ i.ToString();
                root.ChildNodes.Add(node);
            }
            this.TreeView1.Nodes.Add(root);
        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {

        string temp = this.TreeView1.SelectedNode.Value;
        string[] index = temp.Split(new char[] { ',' });
        string current = index[1];
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("XMLFile30.xml"));

        XmlNode node = doc.SelectNodes("//LogPattern")[int.Parse(current)];
    }
  
       <asp:TreeView ID="TreeView1" runat="server"  OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
        </asp:TreeView>
 

Answer6:

WOW! It is exactly what I want. Thank you very much.

Thank you for taking time to write this code.

 


Answer7:

I also find another way. Here you go:

 // To have a index to duplicated element

private int iIndex = 0;protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)

{

// Find the duplicated elements with name "LogPattern"

if (e.Node.Text == "LogPattern" && e.Node.ChildNodes.Count == 0)

{

iIndex++;

e.Node.Text +=
", " + iIndex.ToString();

}

}


Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0