c#读取xml文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12207361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# reading xml file
提问by user516883
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.
我正在尝试<thumbnail_large>http://b.vimeocdn.com/ts/235/662/23566238_640.jpg</thumbnail_large>从创建的 xml 文件中的 thumb_large 元素中提取值。有没有办法在不遍历 who xml 文件的情况下提取该值?谢谢你的帮助。
<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>
采纳答案by Jan
Yes, there is and it is called XPath. Try this:
是的,有,它被称为 XPath。尝试这个:
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.
至少这是我可以从这个格式不佳的文件中读取的内容。如果您使用两种不同的字母(视频详细信息和 HTML 标记),您应该考虑使用命名空间。
回答by Kirill Polishchuk
Look at:
看着:
- For .NET Framework 3.5+ --
XDocument - otherwise:
XmlDocument
- 对于 .NET Framework 3.5+ --
XDocument - 除此以外:
XmlDocument
回答by perilbrain
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));
}

