2013-05-16 2 views
0

mi이 문제는 Medialibrary에서 XML을 얻었지만 몇 가지를 수정해야하지만 strear는 읽기 전용입니까?Sitecore, 미디어 라이브러리에 저장된 XML 수정

당신은 당신이, 당신은 미디어 라이브러리로 다시 수정 된 스트림을 업로드 디스크에있는 파일을 필요로하는 것처럼 문서를 저장하려는

 string nodeId = string.Empty; 
     List<XmlNode> nodes = GetAll(); // Get all the nodes in my XML 

     foreach (XmlNode node in nodes) 
     { 
      nodeId = node.SelectSingleNode(".//id").InnerText; 
      if (nodeId == id) // id the id of the node that i'm looking for 
      { 
       node.ParentNode.RemoveChild(node); 
       node.OwnerDocument.Save(MediaManager.GetMedia(mitem).GetStream().Stream); 
       return; 
      } 
     } 

답변

2

MY CODE :

http://briancaos.wordpress.com/2009/07/09/adding-a-file-to-the-sitecore-media-library-programatically/

본질적으로 기존 문서를 수정하지 않고 새 문서를 업로드한다는 것을 기억하십시오.

다음은 미디어 라이브러리에서 XML 문서를 검색하고 새 노드를 추가 한 다음 다시 같은 위치에 저장하는 샘플 코드입니다.

나는 내가 이미 미디어 라이브러리에서 파일을 가지고있는 파일의 경로를 해달라고 동적에 의해 필요 렸기 때문에 좋은 없습니다
string mediaItemPath = "/sitecore/media library/Files/TestDoc"; 
string fileName = "TestDoc.xml"; 

//get the content db, i.e. master db 
Sitecore.Data.Database contentDB = Sitecore.Context.ContentDatabase ?? Sitecore.Context.Database; 

//get the media file 
MediaItem mi = contentDB.GetItem(mediaItemPath); 

if (mi == null) 
    throw new ItemNotFoundException(mediaItemPath); 

if (!mi.Extension.Equals("xml") || !mi.MimeType.Equals("text/xml")) 
    throw new MediaException(string.Format("File {0} was not of correct XML format", mediaItemPath)); 

//load xml document using media stream 
var xDoc = System.Xml.Linq.XDocument.Load(mi.GetMediaStream()); 

////manipulate the xml as required 
////-- update or add new node or whatever 
string nodeName = "Item"; 
string nodeValue = string.Format("{0} {1}", "test node ", DateTime.Now.ToString("yyyyMMddhhmmss")); 
xDoc.Element("rootNode") 
    .Add(new System.Xml.Linq.XElement(nodeName, nodeValue)); 

//save the modified document into a stream 
var xmlStream = new System.IO.MemoryStream(); 
xDoc.Save(xmlStream); 

// Create the options 
var options = new Sitecore.Resources.Media.MediaCreatorOptions 
        { 
         FileBased = false, // Store the file in the database, not as a file 
         IncludeExtensionInItemName = false, // Keep file extension in item name 
         KeepExisting = false, // Keep any existing file with the same name 
         Versioned = false, // Do not make a versioned template 
         Destination = mediaItemPath, // set the path 
         Database = contentDB // Set the database 
        }; 

//Use a security disabler to allow changes 
using (new Sitecore.SecurityModel.SecurityDisabler()) 
{ 
    //save the item back into media library 
    Item mediaItem = Sitecore.Resources.Media.MediaManager.Creator.CreateFromStream(xmlStream, fileName, options); 
    xmlStream.Dispose(); 
} 
+0

, 당신은 내가 메모리에서 파일을하지 만드는 몇 가지 방법이 필요합니다 aproach folowing from disk – devMd

+0

난 그 제조법을 정확하게 따르는 것을 말하는 것이 아니며 단지 파일을 메모리로 스트리밍하고, 수정 한 다음 미디어 라이브러리에 스트림으로 만들 것을 말하는 것입니다. 'mediaItem = Sitecore.Resources.Media.MediaManager.Creator.CreateFromStream (ms, fileName, options)'는 현재 호출중인 OwnerDOcument.Save 메소드가 아니라 디스크에 저장된 항목에 대해서만 작동합니다. – jammykam

+0

나는 그것을 얻지 못한다. 내가 새로운 스트림에 파일을 연관시키는 방법을 이해할 수 없다. 나는이 je에서 새로운 것이다. – devMd

관련 문제