XmlDocument |更新节点

时间:2010-11-08 20:54:21

标签: c# .net xmldocument updating

好的,所以我开始构建一个Youtube播放器,我有一个XmlDocument对象来存储视频的元信息,但是有一些问题想出如何更新xml文档。

到目前为止我的代码:

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>

如果你们有更好的解决方案来实现这一目标。

先谢谢。

1 个答案:

答案 0 :(得分:0)

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

    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