简体   繁体   English

用C#获取此xml值

[英]get this xml value with c#

I need to get the number next to the word text, in this case the number is 1 我需要在单词文字旁边获取数字,在这种情况下,数字为1

<SD>
<POPULARITY URL="google.com/" TEXT="1"/>
<REACH RANK="1"/>
<RANK DELTA="+0"/>
</SD>

How can I get the number in c# 我如何在C#中获取数字

Thanks 谢谢

In addition to the examples above you could try using linq to xml 除了上面的示例,您还可以尝试使用linq到xml

See below. 见下文。

    var str = @"<ALEXA VER='0.9' URL='google.com/' HOME='0' AID='='>

<SD TITLE='A' FLAGS='DMOZ' HOST='google.com'> 
<TITLE TEXT='Google                             '/> 
<ADDR STREET='' CITY='' STATE='' ZIP='' COUNTRY='' />
<CREATED DATE='15-Sep-1997' DAY='15' MONTH='09' YEAR='1997'/>
<PHONE NUMBER='unlisted'/>
<OWNER NAME='unlisted'/>
<EMAIL ADDR='dns-admin@google.com'/>
<LANG LEX='en'/>
<LINKSIN NUM='704402'/>
<SPEED TEXT='1581' PCT='48'/>
<REVIEWS AVG='4.5' NUM='524'/>
<CHILD SRATING='0'/>
<ASSOCS>
<ASSOC ID='googlecom'/></ASSOCS>
</SD>

<KEYWORDS>
<KEYWORD VAL='Mountain View'/>
</KEYWORDS><DMOZ>
<SITE BASE='google.com/' TITLE='Google' DESC='Enables users to search the Web, Usenet, and images. Features include PageRank, caching and translation of results, and an option to find similar pages. The companys focus is developing search technology.'>
<CATS>
<CAT ID='Top/Computers/Internet/Searching/Search_Engines/Google' TITLE='Search Engines/Google' CID='374822'/>
<CAT ID='Top/Regional/North_America/United_States/California/Localities/M/Mountain_View/Business_and_Economy/Industrial/Computers_and_Internet' TITLE='Industrial/Computers and Internet' CID='625367'/>
<CAT ID='Top/World/Arabic/إقليمـي/الشرق_الأوسط/السعودية/تجارة_و_أقتصاد/كمبيوتر_و_إنترنت/محركات_بحث' TITLE='كمبيوتر و إنترنت/محركات بحث' CID='204954'/>
<CAT ID='Top/World/Français/Informatique/Internet/Recherche/Moteurs_de_recherche/Google' TITLE='Moteurs de recherche/Google' CID='247347'/>
</CATS>
</SITE>
</DMOZ>
<SD>
<POPULARITY URL='google.com/' TEXT='1'/>
<REACH RANK='1'/>
<RANK DELTA='+0'/>
</SD>
</ALEXA>";

    var item = XElement.Parse(str);

    var subSet = item.Elements("SD");

    var actualItem =  subSet.Where(x => x.Element("POPULARITY") != null).First();

    var value = actualItem.Element("POPULARITY").Attribute("TEXT").Value;

Hope this helps 希望这可以帮助

Something like this: 像这样的东西:

XmlDocument doc = new XmlDocument();
doc.LoadXml( @"<SD> <POPULARITY URL=""google.com/"" TEXT=""1""/> <REACH RANK=""1""/> <RANK DELTA=""+0""/> </SD> ");

XmlNode root = doc.FirstChild;

Debug.WriteLine(root["POPULARITY"].Attributes["TEXT"].InnerXml);

You can try: 你可以试试:

XmlDocument doc = new Xmldocument(); 
doc.Load(stringWithYourXml);

XmlNode node = doc.SelectSingleNode("/SD/POPULARITY");
var val = node.Attributes["TEXT"].Value

Please consider this as a sample ( do some more checks and error detection ) 请将此作为示例(进行更多检查和错误检测)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM