2010-11-08 70 views
0

好吧,我即將開始構建一個Youtube播放器,並且我有一個XmlDocument對象來存儲視頻的元信息,但有一些問題需要弄清楚如何更新xml文檔。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); 
    } 
} 

@see上述評論

這基本上是一個小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> 

,如果你們有更好的解決方案,這樣做IM所有的耳朵。

謝謝先進。

+0

有啥問題?它如何不更新? – gideon 2010-12-10 05:16:49

回答

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