user516883
user516883

Reputation: 9378

c# reading xml file

I am trying to pull the <thumbnail_large>http://b.vimeocdn.com/ts/235/662/23566238_640.jpg</thumbnail_large> value from the thumbnail_large element in a created xml file. Is there a way to pull that value without looping through the who xml file? Thanks for any help.

<videos>
    <video>
        <id>6271487</id>
        <title>Spheres</title>
        <description>text</description>
        <url>http://vimeo.com/6271487</url>
        <thumbnail_small>http://b.vimeocdn.com/ts/235/662/23566238_100.jpg</thumbnail_small>
        <thumbnail_medium>http://b.vimeocdn.com/ts/235/662/23566238_200.jpg</thumbnail_medium>
        <thumbnail_large>http://b.vimeocdn.com/ts/235/662/23566238_640.jpg</thumbnail_large>
        <embed_privacy>anywhere</embed_privacy>
    </video>
</videos>

Upvotes: 3

Views: 20650

Answers (3)

perilbrain
perilbrain

Reputation: 8197

string s=File.ReadAllText("My.xml");
List<string> urllist= new List<string>();
string val="<thumbnail_large>";
int start= s.IndexOf(val);
while(start!=-1)
{               
start = s.IndexOf(val,start+val.length);
int end = s.IndexOf("</thumbnail_large>",start);
urllist.add(s.Substring(start, end - start -1));
}

Upvotes: -4

Jan
Jan

Reputation: 2168

Yes, there is and it is called XPath. Try this:

XmlDocument doc = new XmlDocument();
doc.Load(@"/path/to/file");
XmlNode node = doc.SelectSingleNode("/videos/video/thumbnail_large");
string URI = node.InnerText;

At least that is what I can read from this poorly formatted file. If you are using two different alphabets (video details and HTML markup) you should consider using namespaces.

Upvotes: 9

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Look at:

  1. For .NET Framework 3.5+ -- XDocument
  2. otherwise: XmlDocument

Upvotes: 4

Related Questions