locked
Get data from xml file in c# RRS feed

  • Question

  • <forum>
        <HD720p HD720p="89" />
      <HD1080p HD1080p="89" />
      <TrueHD TrueHD="119" />  
    </forum>

    I have made above xml as database so how do i get value when i choose the Attribute from comboBox1? I.e comboBox1 contains itemes same as database items. So when i select from comboBox1 like HD720p then its value 89 should be displayed in textbox1.

    Please suggest me.


    • Edited by WinFORs Monday, June 25, 2012 6:18 PM
    Monday, June 25, 2012 6:17 PM

Answers

  • Hi, 

    Check with 

    			var xDoc = new XmlDocument();
    			xDoc.Load("xmlcontent");
    			var node = xDoc.DocumentElement.SelectSingleNode("HD720p");
    			//select node value
    			var value = node.Attributes["HD720p"].Value;

    Hope this helps you....


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    • Marked as answer by WinFORs Monday, June 25, 2012 7:07 PM
    Monday, June 25, 2012 6:58 PM
  • Yes. By using Xml.Linq you can do:

    XDocument doc = XDocument.Load("you path to xml file");
    var query = doc.Descendants("forum").Select(s=> new 
    {
        HD720p = s.Element("HD720p").Value
    }).ToList();
    
    string myValue = query[0].HD720p;


    Mitja

    • Marked as answer by WinFORs Monday, June 25, 2012 7:07 PM
    Monday, June 25, 2012 6:59 PM

All replies

  • Hi,

    but shouldn`t xml file be like:

    <forum>
      <HD720p>=89</HD720p>
      <HD1080p>89</HD1080p>
      <TrueHD>119</TrueHD>
    </forum>

    ... so that it has start and end tags.


    Mitja

    Monday, June 25, 2012 6:40 PM
  • ok for your xml file can you show me how can i get this what i want?

    Thank you so much.

    Monday, June 25, 2012 6:44 PM
  • Hi, 

    Check with 

    			var xDoc = new XmlDocument();
    			xDoc.Load("xmlcontent");
    			var node = xDoc.DocumentElement.SelectSingleNode("HD720p");
    			//select node value
    			var value = node.Attributes["HD720p"].Value;

    Hope this helps you....


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    • Marked as answer by WinFORs Monday, June 25, 2012 7:07 PM
    Monday, June 25, 2012 6:58 PM
  • Yes. By using Xml.Linq you can do:

    XDocument doc = XDocument.Load("you path to xml file");
    var query = doc.Descendants("forum").Select(s=> new 
    {
        HD720p = s.Element("HD720p").Value
    }).ToList();
    
    string myValue = query[0].HD720p;


    Mitja

    • Marked as answer by WinFORs Monday, June 25, 2012 7:07 PM
    Monday, June 25, 2012 6:59 PM
  • Hi, 

    Or

    var xPathDocument = new XPathDocument(@"path to xml");
    var navigator = xPathDocument.CreateNavigator();
    var value = navigator.SelectSingleNode("//HD720p").Value;


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Monday, June 25, 2012 7:01 PM
  • Thank you so much for this sweet help.:)
    Monday, June 25, 2012 7:08 PM