tags:

views:

33

answers:

2

Ihave an xml file sample.xml

i need to display number from above xml file in a listview named _listView which contain version column named _version,can you give me the optimized code required to do that task

+1  A: 
using System.Xml.Linq;


string xml = ...
string version = XElement.Parse(xml).Element("ManagedObject").Attribute("version").Value;

There is an example on using a listview here

Preet Sangha
Can you complete this answer
peter
can you complete it up to displaying in to listview regarding the question
peter
i am using version2.0 ,,i need a complete coding to view it in listview as per the question what i gave in 2.0 version though i am a beginner
peter
A: 

How about:

XNamespace ns = "http://tempuri.org/SpoDataSchema.xsd";
string version = (string)XDocument.Load("sample.xml").Root
     .Element(ns + "ManagedObject").Element(ns + "version");

Or in 2.0:

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

Then display version however you want. For displaying in a ListView:

using (Form form = new Form())
using (ListView lv = new ListView())
{
    lv.Dock = DockStyle.Fill;
    lv.View = View.Details;
    lv.Columns.Add("Version");
    lv.Items.Add(version);
    form.Controls.Add(lv);
    form.ShowDialog();
}
Marc Gravell
regarding XNamespace namespace its giving error assembly refernce or something
peter
You will need .NET 3.5 and System.Xml.Linq.dll; if you are in 2.0/3.0 I can provide a 2.0-friendly version.
Marc Gravell
yes i need a complete coding to view it in listview as per the question what i gave in 2.0 version
peter
but i am not able to see anything in listview
peter
That is just a namespace; it doesn't get used. The `ListView` code works as presented; if it doesn't show for you, you're going to have to indicate what you are doing differently.
Marc Gravell