Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I have an xml file like this:

XML
<document>
    <Clients>
        <Client>
            <ClientDetails>
                <clientName>John</clientName>
            </ClientDetails>
            <properties>

                <Property>
                    <id>199</id>
                    <Address>
                        <street>1st Street</street>
                    </Address>
                    <Price>
                        <price>10000</price>
                    </Price>
                </Property>

                <Property>
                    <id>200</id>
                    <Address>
                        <street>2st Street</street>
                    </Address>
                    <Price>
                        <price>20000</price>
                    </Price>
                </Property>

            </properties>
        </Client>
    </Clients>
</document>


I would like to list all the IDs in a listView. I made this and it works.

When I double click one of the ID I would like to show its details in several textboxes (for example). So I would have a textbox of street info, another textbox with price info, etc.

This is how I did the first listing:

C#
DataSet ds = new DataSet();
ds.ReadXml(@"http://myUrl.com/Default.xml");
ListViewItem item;
foreach(DataRow dr in ds.Tables["Property"].Rows)
    {
        item = new ListViewItem(new string[]
        {
            dr["id"].ToString(),
        });
        listView1.Items.Add(item);
    }

I couldn't find any online solution for this kind of xml search.

Many thanks for your help.



UPDATE:

I managed to list every info from a childnode by selecting one ID in listview. But this way I get the result as one string:

C#
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    string propertyid = listView1.SelectedItems[0].Text;
    XmlDocument doc = new XmlDocument();
    doc.Load(@"http://myUrl.com/Default.xml");
    XmlNode nodes = doc.SelectSingleNode("document/Clients/Client/properties/Property[id[text()='" + propertyid + "']]");

    foreach(XmlNode node in nodes)
    {
        textBox1.Text += node.InnerText + "\r\n";
    }

}


Still would like to separate the results into several strings... :(
Posted
Updated 15-Sep-15 4:58am
v2
Comments
F-ES Sitecore 15-Sep-15 11:27am    
Use doc.SelectNodes rather than SelectSingleNode. That will return a NodeList you can iterate through.
DoomMaker79 16-Sep-15 2:03am    
thanks for your reply. please check my solution and tell me what you think please.

1 solution

Thank you F-ES Sitecore, you gave me the right direction. This is may almost-done-solution:

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
   string propertyid = listView1.SelectedItems[0].Text;
   XmlDocument doc = new XmlDocument();
   doc.Load(@"http://myUrl.com/Default.xml");

   XmlNodeList nodes = doc.SelectNodes("document/Clients/Client/properties/Property");

   foreach(XmlNode node in nodes)
   {
      if(node.SelectSingleNode("id").InnerText == propertyid)
      {
           XmlNodeList addresses = node.SelectNodes("Address");
           foreach (XmlNode address in addresses)
           {
                streetBox.Text = address.SelectSingleNode("street").InnerText;
           }

           XmlNodeList prices = node.SelectNodes("Price");
           foreach(XmlNode price in prices)
           {
                priceBox.Text = price.SelectSingleNode("price").InnerText;
           }
       }
   }
}



My only problem is, that this solution is quite slow... I guess it is because of those foreach-es and the long XML source.

Do you have any suggestion how to optimize this code part?

Sorry for my stupid questions, but I'm really new to C#. Am I right with this pairing:

XmlNodeList class -> should use SelectNodes
XmlNode class -> should use SelectSingleNode

?

Thank you
 
Share this answer
 
Comments
F-ES Sitecore 16-Sep-15 4:06am    
If that is your literal xml then there is no point using selectnodes where there is only one child item. You can use select node and get the price node using SelectSinglerNode with the path "Price\price"
DoomMaker79 16-Sep-15 5:21am    
yea, but there are several child nodes under all nodes. I just mentioned only 1 to make it simple.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900