如何使用C#Asp.net从此XML字符串中提取URL

时间:2012-02-28 11:59:44

标签: c# asp.net xml parsing

我正在使用Imgur api从我的webapp上传图片。如何使用C#Asp.net

从此XML字符串的标记中提取URL
 <upload>
      <image>
        <name />
        <title />
        <caption />
        <hash>MkUDH</hash>
        <deletehash>kP5lIWWU0vDqrO1</deletehash>
        <datetime>2012-02-28 11:47:25</datetime>
        <type>image/jpeg</type>
        <animated>false</animated>
        <width>640</width>
        <height>480</height>
        <size>173578</size>
        <views>0</views>
        <bandwidth>0</bandwidth>
      </image>
      <links>
        <original>http://i.imgur.com/MkUDH.jpg</original>
        <imgur_page>http://imgur.com/MkUDH</imgur_page>
        <delete_page>http://imgur.com/delete/kP5lIWWU0vDqrO1</delete_page>
        <small_square>http://i.imgur.com/MkUDHs.jpg</small_square>
        <large_thumbnail>http://i.imgur.com/MkUDHl.jpg</large_thumbnail>
      </links>
    </upload>

3 个答案:

答案 0 :(得分:1)

您可以尝试Linq Xml读取xml文档并提取元素值。

 XDocument doc = XDocument.Load(file);
 var result = doc.Root.Element("links").Element("original").Value;

答案 1 :(得分:0)

XmlDocument xml = new XmlDocument();
xml.LoadXml(myXmlString); // your XML String

XmlNodeList xnList = xml.SelectNodes("/upload/links");
foreach (XmlNode xn in xnList)
{
  string original= xn["original"].InnerText;
  string imgur_page = xn["imgur_page"].InnerText;
  string delete_page = xn["delete_page"].InnerText;
  string small_square = xn["small_square"].InnerText;
  string large_thumbnail= xn["large_thumbnail"].InnerText;


}

答案 2 :(得分:0)

var url = Regex.Replace(s, @".*original\>(.*)\<\/original\>.*", "$1")

输出:

http://i.imgur.com/MkUDH.jpg