Xml文档 |更新节点
我开始构建一个 Youtube 播放器,并且有一个 XmlDocument 对象来存储视频的元信息,但我在弄清楚如何更新 XmlDocument 时遇到了一些问题。
这是到目前为止我的代码:
public void UpdateVideo(string video_id, string title, string download_url)
{
if (this.DownloadExists(video_id))
{
XmlNodeList Videos = Document.GetElementsByTagName(video_id);
if (Videos.Count == 1)
{
XmlNode Video = Videos[0];
//Update the Title
XmlNodeList Properties = Video.ChildNodes;
//Title
foreach (XmlNode Property in Properties)
{
switch (Property.Name.ToLower())
{
case "title":
Property.InnerText = title;
break;
case "download_url":
Property.InnerText = download_url;
break;
}
//Update the property back to Video object......
//Update the Video back to the Videos etc.......
}
}
Document.Save(StorageFile);
}
}
这基本上是一个小型 VideoStorage
类,用于读取/写入 Xml 文档。
示例 XML 数据如下所示:
<?xml version="1.0" encoding="iso-8859-1"?>
<videos>
<pqky5B179nM>
<id>pqky5B179nM</id>
<title>will.i.am, Nicki Minaj - Check It Out</title>
<videod_url>http://www.youtube.com/watch?v=pqky5B179nM</videod_url>
</pqky5B179nM>
</videos>
有更好的方法吗?
I'm starting to build a Youtube player and I have an XmlDocument object to store the video's meta information, but I'm having some issues figuring out how to update the XmlDocument.
Here's my code so far:
public void UpdateVideo(string video_id, string title, string download_url)
{
if (this.DownloadExists(video_id))
{
XmlNodeList Videos = Document.GetElementsByTagName(video_id);
if (Videos.Count == 1)
{
XmlNode Video = Videos[0];
//Update the Title
XmlNodeList Properties = Video.ChildNodes;
//Title
foreach (XmlNode Property in Properties)
{
switch (Property.Name.ToLower())
{
case "title":
Property.InnerText = title;
break;
case "download_url":
Property.InnerText = download_url;
break;
}
//Update the property back to Video object......
//Update the Video back to the Videos etc.......
}
}
Document.Save(StorageFile);
}
}
This is basically a small VideoStorage
Class, that reads/writes to an Xml Document.
Sample XML Data is like so:
<?xml version="1.0" encoding="iso-8859-1"?>
<videos>
<pqky5B179nM>
<id>pqky5B179nM</id>
<title>will.i.am, Nicki Minaj - Check It Out</title>
<videod_url>http://www.youtube.com/watch?v=pqky5B179nM</videod_url>
</pqky5B179nM>
</videos>
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这对您来说更具可读性:
Perhaps this is more readable for you: