簡體   English   中英

無法將更改保存到存儲在Sharepoint 2010文檔庫中的XML文檔

[英]Unable to save changes to XML document stored in Sharepoint 2010 Document Library

我正在一個需要將所有SQL連接和查詢信息存儲在XML文件中的項目。 為了使我的項目可配置,我試圖創建一種方法,使用戶可以通過一系列文本框配置其sql連接字符串信息(數據源,目錄,用戶名和密碼)。 然后,此輸入將保存到SQL文檔中的相應節點。

我可以從XML文件中獲取當前信息,並在文本框中顯示該信息以供用戶檢查和更正,但是在保存更改時遇到了錯誤。

這是我用來更新和保存xml文檔的代碼。

protected void submitBtn_Click(object sender, EventArgs e)
    {
        SPFile file = methods.web.GetFile("MyXMLFile.xml");
        myDoc = new XmlDocument();
        byte[] bites = file.OpenBinary();
        Stream strm1 = new MemoryStream(bites);
        myDoc.Load(strm1);

        XmlNode node;
        node = myDoc.DocumentElement;

        foreach (XmlNode node1 in node.ChildNodes)
        {
           foreach (XmlNode node2 in node1.ChildNodes)
           {
               if (node2.Name == "name1")
               {
                   if (node2.InnerText != box1.Text)
                   {

                   }
               }

               if (node2.Name == "name2")
               {
                   if (node2.InnerText != box2.Text)
                   {

                   }
               }

               if (node2.Name == "name3")
               {
                   if (node2.InnerText != box3.Text)
                   {
                       node2.InnerText = box3.Text;
                   }
               }

               if (node2.Name == "name4")
               {
                   if (node2.InnerText != box4.Text)
                   {

                   }
               }
           }
        } 

        myDoc.Save(strm1);
    }

由於我仍在測試中,因此大多數條件在這一點上都是空的。

如我所說,該代碼在最后一行之前都有效。 那時,我收到錯誤消息“內存流不可擴展”。 我知道使用內存流更新存儲的文件是不正確的,但是我不知道執行此操作的正確方法。

我試圖實現類似問題的解決方案,在“ 內存流”是不可擴展的,但是這種情況與我的情況不同,因此該實現對我來說沒有任何意義。 任何澄清將不勝感激。

使用將字節數組作為參數的MemoryStream構造函數,將創建MemoryStream的不可調整大小的實例。 由於您正在更改文件(因此更改了基礎字節),因此需要可調整大小的MemoryStream 這可以通過使用MemoryStream類的無參數構造函數並將字節數組寫入MemoryStream

嘗試這個:

    SPFile file = methods.web.GetFile("MyXMLFile.xml");
    myDoc = new XmlDocument();
    byte[] bites = file.OpenBinary();

    using(MemoryStream strm1 = new MemoryStream()){
         strm1.Write(bites, 0, (int)bites.Length);
         strm1.Position = 0;
         myDoc.Load(strm1);

         // all of your edits to the file here
         strm1.Position = 0;

         // save the file back to disk
         using(var fs = new FileStream("FILEPATH",FileMode.Create,FileAccess.ReadWrite)){
              myDoc.Save(fs);
         }
    }

要獲取Sharepoint文件的FILEPATH ,需要遵循以下步驟(我現在沒有設置Sharepoint開發環境):

SPFile file = methods.web.GetFile("MyXMLFile.xml")
var filepath = file.ParentFolder.ServerRelativeUrl + "\\" + file.Name;

或者像這樣僅使用SPFile類的SaveBinary方法可能會更容易:

  // same code from above

  // all of your edits to the file here
  strm1.Position = 0;
  // don't use a FileStream, just SaveBinary
  file.SaveBinary(strm1);

我沒有測試此代碼,但已在Sharepoint解決方案中使用它來修改Sharepoint列表中的XML(主要是OpenXML)文檔。 閱讀此博客文章以獲取更多信息

您可以考慮使用XDocument類而不是XmlDocument類。 http://msdn.microsoft.com/zh-CN/library/system.xml.linq.xdocument.aspx

我更喜歡它,因為它簡單易用,並且無需使用內存流。

編輯 :您可以像這樣追加到文件:

XDocument doc = XDocument.Load('filePath');
                doc.Root.Add(
                    new XElement("An Element Name",
                        new XAttribute("An Attribute", "Some Value"),
                        new XElement("Nested Element", "Inner Text"))
                    );
                doc.Save(filePath);

或者,您可以搜索元素並進行如下更新:

doc.Root.Elements("The element").First(m => 
     m.Attribute("An Attribute").Value == "Some value to match").SetElementValue(
          "The element to change", "Value to set element to");
doc.Save('filePath');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM