开发者

Get XML attribute with C#

开发者 https://www.devze.com 2023-02-05 23:30 出处:网络
I have an XML file like the following. <div class=\"time\"> <span cl开发者_运维问答ass=\"title\">Bla: </span>

I have an XML file like the following.

<div class="time">
   <span cl开发者_运维问答ass="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


Sounds like you rather need an HTML Parser IMHO. if so, then Take a look at Html Agility Pack


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


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());
}


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

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

Cheers, Matthias


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: /div/span[@class="value"]

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. Link here


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();


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));


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消