Xml文档 |更新节点

发布于 2024-10-01 01:46:01 字数 1573 浏览 1 评论 0原文

我开始构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-10-08 01:46:01

也许这对您来说更具可读性:

    Private Sub SaveItem(ByVal Title As String, ByVal GroupData As String)
            'Save Data in an XML file (Timo Böhme, www.goldengel.ch)
            Dim fi As New IO.FileInfo(TB.SystemMain.AppPath & "ButtonLayout.xml") 'Define the file to write in
            Dim writer As New Xml.XmlTextWriter(fi.FullName, System.Text.Encoding.UTF8) 'create new XML reader class


            writer.WriteStartDocument() 'start writing Xml document

                    writer.WriteStartElement("PositionInfos") 'go or create to PositionInfos tag

                    writer.WriteStartElement("PositionInfo") 'go or create to PositionInfo tag
                            writer.WriteAttributeString("Title", Title) 'write attribut Title
                            writer.WriteAttributeString("GroupData", GroupData) 'write attribut GroupData
                    writer.WriteEndElement() 'close PositionInfo tag

            writer.WriteEndElement() 'close PositionInfos tag
            writer.WriteEndDocument() 'close document tag

            writer.Flush() 'write to disk
            writer.Close() 'close file
    End Sub

Perhaps this is more readable for you:

    Private Sub SaveItem(ByVal Title As String, ByVal GroupData As String)
            'Save Data in an XML file (Timo Böhme, www.goldengel.ch)
            Dim fi As New IO.FileInfo(TB.SystemMain.AppPath & "ButtonLayout.xml") 'Define the file to write in
            Dim writer As New Xml.XmlTextWriter(fi.FullName, System.Text.Encoding.UTF8) 'create new XML reader class


            writer.WriteStartDocument() 'start writing Xml document

                    writer.WriteStartElement("PositionInfos") 'go or create to PositionInfos tag

                    writer.WriteStartElement("PositionInfo") 'go or create to PositionInfo tag
                            writer.WriteAttributeString("Title", Title) 'write attribut Title
                            writer.WriteAttributeString("GroupData", GroupData) 'write attribut GroupData
                    writer.WriteEndElement() 'close PositionInfo tag

            writer.WriteEndElement() 'close PositionInfos tag
            writer.WriteEndDocument() 'close document tag

            writer.Flush() 'write to disk
            writer.Close() 'close file
    End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文