views:

64

answers:

1

I'm having an xml file like

<Root>
 <Child val1="1" val2="2"/>
 <Child val1="1" val2="3"/>
 <Child val1="2" val2="4"/>
</Root>

i need to display the data from the Xml file to a Listview like

alt text

(Added A to index value)

Now i'm using like

1.Stores the data in an XmlNodesList

2.Then iterate through the nodeslist and add the attribute value to the list view

Here i can not use Dictionary<String,String> as a temporary storage because there exist multiple keys with same name.

Is there any idea to do this using LINQ to XML.?

+1  A: 

Without LINQ:

var doc = new System.Xml.XmlDocument();
doc.LoadXml(xml);

var nodes = doc.SelectNodes("Root/Child");

for (int i = 0; i < nodes.Count; i++)
{
    var n = nodes[i];
    var index = String.Format("A{0}", i + 1);
    var column1 = n.Attributes["val1"].Value;
    var column2 = n.Attributes["val1"].Value;

    // use variables to add an item to ListView
}

Using LINQ:

using System.Linq;

var doc = new System.Xml.XmlDocument();
doc.LoadXml(xml);

var nodes = doc.SelectNodes("Root/Child");
var arr = nodes
    .OfType<XmlNode>()
    .ToArray();

var result = arr
    .Select(n =>
        new
        {
            ClNo = String.Format("A{0}", Array.IndexOf(arr, n) +1),
            Val1 = n.Attributes["val1"].Value,
            Val2 = n.Attributes["val2"].Value,
        });

ListView list = new ListView();
ListViewItem[] items = result
    .Select(r => new ListViewItem(new[] { r.ClNo, r.Val1, r.Val2 })
    .ToArray();
list.Items.AddRange(items);
abatishchev
@abatishchev : `Populate()` - i do not understand this
Pramodh
I mean that `Populate()` returns `XmlNodeList` data from your XML file, e.g. `doc.SelectNodes("root/child");`
abatishchev
@abatishchev: Thank you , but its throwing an error like `Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.` i'm using .NET 3.5 frame work
Pramodh
@Pramodh: Check my updated post
abatishchev
@ abatishchev : No also its throwing the same error. I think we can not use ListView property( ColumnName) as an anonymous type variable
Pramodh
@Pramodh: What error and what string? The code works fine for me!
abatishchev
@ abatishchev : You are right. i thought the `query` directly dump the data to the list view. Here we need to iterate through `result` to dump the data to the list view . Am i right??
Pramodh
@Pramodh: If use anonymous type and lambda - yes
abatishchev
@Pramodh: See my updated post how to do that without them.
abatishchev
@ abatishchev : The `Without LINQ` is some what like what i'm using now.
Pramodh
@Pramodh: See my updated post how to do add items using LINQ
abatishchev
@Pramodh: How does it go?
abatishchev
@ abatishchev : its throwing some error in `new ListViewItem(r.ClNo, r.Val1, r.Val2) ` like `The best overloaded method match for 'System.Windows.Forms.ListViewItem.ListViewItem(string, string, System.Windows.Forms.ListViewGroup)' has some invalid arguments`
Pramodh
@Pramodh: I edited my post. I thought that constructor accepts `params string[]` but indeed it accepts just `string[]`
abatishchev
@ abatishchev : wow!!! ... superb... its working fine .... thank you..:-)
Pramodh
@abatishchev : how about this `int i = 1;ListViewItem[] item = XDocument.Load(Application.StartupPath + "\\foo.xml").Descendants("Child").Select(X => new{a = X.Attribute("val1").Value,b = X.Attribute("Val2").Value}).Select(X => new ListViewItem(new String[] {"A"+(i++).ToString(), X.a, X.b})).ToArray(); listView1.Items.AddRange(item);`
Pramodh