views:

86

answers:

2

Ihave an xml file sample.xml

<?xml version="1.0" standalone="yes"?>
<DataSchema xmlns="http://tempuri.org/DataSchema.xsd"&gt;
  <ManagedObject>
    <Label>sam</Label>
    <Owner>00000000-0000-0000-0000-000000000000</Owner>   
    <ClassID>00000000-0000-0000-0000-000000000008</ClassID>
    <DefaultApp>00000000-0000-0000-0000-000000000000</DefaultApp>
    <Name>rvi</Name>
    <version>9.6</version>
  </ManagedObject>
</DataSchema>

i need to display 9.6 (9.6) from above xml file to the listview.I started a new windowform application. i added listview named "_listview" put its view as a "detail" mode.I added a column there named _version Can you give me the code to display version number in the listview column

+1  A: 

if your XML file will have always this structre you can use simply:

        string version = "";
        int n = 0;

        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(@"sample.xml");
            if(ds.Tables.Contains("ManagedObject") 
               && ds.Tables["ManagedObject"].Rows.Count > n)
            {
                ver = ds.Tables["ManagedObject"].Rows[n]["version"].ToString();
            }
        }

to get the n-th ManagedObject version.

in your case you have only 1, so n = 0.

if you want to add "version" as an item that will appear in the first column of the listview:

        listView1.Items.Add(version);

if "version" is to be added to an existing item in a secondary column use:

        listview1.Items[n].SubItems.Add(version);

you can look here for further details.

najmeddine
Can you give me the complete code up to listview from the information i gave in my question that will be good for me like begginner
peter
as per the logic it will work,,but how to show in listview something _listview.items.add like that
peter
i got it sorry to disturb u,,_listview.Items.Add(version); will give
peter
i posted one more answer najmeddine,, can you check it,,it will also work
peter
yes it's working.
najmeddine
A: 

this will also work fine

XmlDocument doc = new XmlDocument();
        doc.Load(@"F:\xml\sample.xml");
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
        nsmgr.AddNamespace("sam", "http://tempuri.org/DataSchema.xsd");
        XmlNode node = doc.SelectSingleNode(
            "/sam:DataSchema/sam:ManagedObject/sam:version", nsmgr);
        string version = node == null ? null : node.InnerText;

        _listview.Items.Add(version);
peter