11889

Get XML attribute with C#

Question:

I have an XML file like the following.

<div class="time"> <span class="title">Bla: </span> <span class="value">Thu 20 Jan 11</span> </div>

How can I get value "Thu 20 Jan 11" with C#? thanks in advance

Answer1:

Sounds like you rather need an HTML Parser IMHO. if so, then Take a look at <a href="http://htmlagilitypack.codeplex.com/" rel="nofollow">Html Agility Pack</a>

Answer2:

Given that you do have an XML file like you say, then you need could load the file into an XmlDocument and find what you want using XPath:

class Program { static void Main(string[] args) { var xml = "<div class=\"time\">" + "<span class=\"title\">Bla: </span>" + "<span class=\"value\">Thu 20 Jan 11</span>" + "</div>"; var document = new XmlDocument(); try { document.LoadXml(xml); } catch (XmlException xe) { // Handle and/or re-throw throw; } var date = document.SelectSingleNode("//span[@class = 'value']").InnerText; Console.WriteLine(date); Console.ReadKey(); } }

Output: Thu 20 Jan 11

Answer3:

I wrote a little snippet, which does it for you...

public void Test(String source) { XElement elem = XElement.Parse(source); var query = (from x in elem.Descendants("span") select x.Value).LastOrDefault(); Console.WriteLine(query.ToString()); }

Answer4:

Using XPath queries may be an elegant solution too. See this knowledge base article for a brief how-to: <a href="http://support.microsoft.com/kb/308333" rel="nofollow">http://support.microsoft.com/kb/308333</a>

This of course requires the document to be strictly correct XML, which XHTML is. Unfortunately HTML input often contains syntax errors...

Cheers, Matthias

Answer5:

As said you could parse it as HTML.

However treating it as a XML document you can read the value from the node by using the XPath: <strong>/div/span[@class="value"]</strong>

You can also use XDocument to select a node value from a known XPath or by searching through descendant nodes. Using LINQ this becomes very easy to match on attribute value. <a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx" rel="nofollow">Link here</a>

Answer6:

This is as sgrassie's answer but using linq to xml, I like more this code, but is up to you.

string xml = "<div class=\"time\"><span class=\"title\">Bla: </span><span class=\"value\">Thu 20 Jan 11</span></div>"; StringReader sr = new StringReader(xml); XDocument xdoc = XDocument.Load(sr); var date = xdoc.Element("div").Elements("span").Where(m => ((string)m.Attribute("class")) == "value").FirstOrDefault(); Console.WriteLine(date.Value); Console.ReadLine();

Answer7:

Below is the code in VTD-XML:

VTDGen vg = new VTDGen(); System.Text.Encoding eg = System.Text.Encoding.GetEncoding("UTF-8"); String XML = "<div class=\"time\">" + "<span class=\"title\">Bla: </span>" + "<span class=\"value\">Thu 20 Jan 11</span>" + "</div>"; vg.setDoc(eg.GetBytes(XML)); vg.parse(true); VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); ap.selectXPath("/div/span[@class='value']/text()"); int i = ap.evalXPath(); if (i!=-1) Console.WriteLine(vn.toString(i));

Answer8:

Ok, guyes I am putting the fragment of the code. The problem is that when I use XPath: //@* I get all the list correctly. Also I tried //@class and it returned the all the class values - OK. But when I put //span[@class='value'] i got blank list. Also I've tried several variations and it seems that when I put attribute equal to something //title[@type='html'] I am getting blank list.

<feed xmlns="w3.org/2005/Atom"> <updated>2011-01-20T08:33:23Z</updated> <title type="html">grgrgr</title> <entry> <title type="html">Blog post : Estiatoria</title> <content type="xhtml"> <div xmlns="w3.org/1999/xhtml"> <div class="due"> <span class="title">Due:</span> <span class="value">20 Jan 11</span> </div> </div> </content> </entry> </feed>

Recommend

  • VB.Net, Linq to Xml: Is this the correct way to use logic while creating xml?
  • Can't edit my datagridview after binding to XML file via linq
  • What is a good way to read this XML?
  • how to extract text from a html element by id and assign to a php variable?
  • Prevent XmlSerializer from auto instantiating List's on Deserialize
  • XML delete node according to timestamp C#
  • I'm moving from C to C++ and I don't get this creation of a class
  • Google directions api web service - show the response on map. Is it possible?
  • Dealing with XElement null value
  • how to pass class attribute and value to markdown syntax
  • Receive list of elements in their visual order
  • How can I sync the html/session used between Django's 'Client' and Selenium's we
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Display issues when we change from one jquery mobile page to another in firefox
  • Master page gives error
  • Deselecting radio buttons while keeping the View Model in synch
  • Counter field in MS Access, how to generate?
  • JSON with duplicate key names losing information when parsed
  • Javascript + PHP Encryption with pidCrypt
  • Display Images one by one with next and previous functionality
  • Websockets service method fails during R startup
  • ORA-29908: missing primary invocation for ancillary operator
  • Jquery - Jquery Wysiwyg return html as a string
  • How to get next/previous record number?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • SQL merge duplicate rows and join values that are different
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How do you join a server to an Active Directory (domain)?
  • JTable with a ScrollPane misbehaving
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?