mi问题是这个,我在Medialibrary中得到了一个XML,我需要修改一些东西但是strear只读,有什么想法吗?
我的代码
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;
}
}
答案 0 :(得分:2)
您只是想像保存磁盘上的文件一样保存文档,您需要将修改后的流上传回媒体库:
请记住,从本质上讲,您要上传新文档,而不是修改现有文档。
以下是从媒体库中检索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();
}