C#XmlSerialization:添加包装 [英] C# XmlSerialization: Adding a wrapper
问题描述
我改变我的序列化应用程序中的数据的方法。到现在为止我手工做的一切,创造了大量的code,而且还答应了我一个很大的灵活性。
I am changing the approach of serializing data inside my application. Until now I did everything manually, which created a lot of code, but also granted me a lot of flexibility.
现在我使用的是的XmlSerializer
,并且为了提供数据的方式我想创建一个DataTransferModel。
Now I am using the XmlSerializer
, and created a DataTransferModel in order to provide the data in the way I want.
这是得到我的路现在唯一的事情是,我需要有各地的XML内容的包装。它仅仅是包含应用程序的名称的其他标记。如何添加各地的应用程序中创建XML的文档这个标签?
The only thing that is getting in my way now is that I need to have a wrapper around the XML-Content. It is just an other tag containing the name of the application. How can I add this tag around the XML-Documents created inside the application?
感谢,如果你需要更多信息,并不hestiate问。
Thanks and if you need more information do not hestiate to ask.
编辑:标签应该是文件中的第一个标签
The tag should be the first tag in the document.
推荐答案
所以,我用改变一个自定义的方法解决了这个问题的的XmlDocument
,我的愿望。
So, I solved this problem using a custom method that changes the XmlDocument
as I wish.
我基本上是创建一个方法,prepares的的XmlDocument
,增加了包装,标签周围的原单。这种方法可以称为每次之前,我必须使用的XmlDocument
。
I basically created a method that "prepares" the XmlDocument
, adding a the wrapper-Tag around the original one. This method can be called every time before I have to use the XmlDocument
.
/// <summary>
/// Prepares the document to export it.
/// Adds the Wrapper tags
/// </summary>
/// <param name="contentDocument">The Document to prepare.</param>
/// <returns>Returns the prepared document.</returns>
private static XmlDocument PrepareExportDocument(XmlDocument contentDocument)
{
XmlDocument returnDoc = new XmlDocument();
XmlNode rootElement = returnDoc.CreateElement("Wrapper-Tag");
XmlNode importedNode = returnDoc.ImportNode(contentDocument.DocumentElement, true);
rootElement.AppendChild(importedNode);
returnDoc.AppendChild(rootElement);
return returnDoc;
}
如果你有更好的办法,或者你觉得这种做法是不正确的随意评论或添加自己的答案。现在因为接受我将标志着这个答案。
If you have better approaches, or you feel this approach is not correct feel free to comment or add your own answers. For now I will mark this answer as accepted.
这篇关于C#XmlSerialization:添加包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!