2

I need to get the number next to the word text, in this case the number is 1

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

How can I get the number in c#

Thanks

0

3 Answers 3

5

In addition to the examples above you could try using linq to 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='[email protected]'/>
<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

Sign up to request clarification or add additional context in comments.

7 Comments

i'm getting System.Exception._COMPlusExceptionCode -532459699 in var value = item.Element("POPULARITY").Attribute("TEXT").Value; same above, don't know why
@robert i'm not sure why u are getting that error. are you sure its related to the code?
str = "\n</DMOZ>\n<SD>\n<POPULARITY URL=\"google.com/\" TEXT=\"1\"/>\n<REACH RANK=\"1\"/>" var item = XElement.Parse(@str); var value = item.Element("POPULARITY").Attribute("TEXT").Value; yes, is relate to that code, here in the next line i'm getting that error string value2 = item.Element("POPULARITY").Attribute("TEXT").Value; the same happened with the other solution like this Debug.WriteLine(root["POPULARITY"].Attributes["TEXT"].InnerXml); pretty weird error, not sure maybe is xml related
@robert Could be an xml error ... hard to say ... could you post the entire xml data (if possible)
i can write 600 letters only, the xml is what you get in this url: data.alexa.com/data?cli=10&dat=s&url=google.com here the last lines: recherche/Google\" CID=\"247347\"/>\n</CATS>\n</SITE>\n</DMOZ>\n<SD>\n<POPULARITY URL=\"google.com/\" TEXT=\"1\"/>\n<REACH RANK=\"1\"/>\n<RANK DELTA=\"+0\"/>\n</SD>\n</ALEXA>"
|
2

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

2 Comments

i'm getting System.Exception._COMPlusExceptionCode -532459699 in Debug.WriteLine(root["POPULARITY"].Attributes["TEXT"].InnerXml); don't know why
yeah the weird error is not xml error, is some weird error i have never got, every error in the code, it show COMPlusExceptionCode
0

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 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.