如何从对象列表创建XML文件 [英] How to create xml file from a list of objects
问题描述
我定义了3类:
public class PublishedPage
{
public string Action { get; private set; }
public string PageGuid { get; set; }
public List<string> SearchableProperties { get; set; }
public PublishedPage()
{
Action = "Published";
SearchableProperties = new List<string>();
}
}
public class DeletedPage
{
public string Action { get; private set; }
public string PageGuid { get; set; }
public DeletedPage()
{
Action = "Deleted";
}
}
public class MovedPage
{
public string Action { get; private set; }
public string PageGuid { get; set; }
public string OldParentGuid { get; set; }
public string NewParentGuid { get; set; }
public MovedPage()
{
Action = "Moved";
}
}
某处在代码中,我有这样的事情:
Somewhere in code I have something like this:
List<PublishedPage> publishedPages = GetPublishedPages();
List<MovedPage> movedPages = GetMovedPages();
List<DeletedPage> deletedPages = GetDeletedPages();
现在我想创建一个包含这3集的XML文件,但不知道怎么办。
XML应该是这样的:
Now I want to create a XML file containing these 3 collections but don't know how.
XML should be like this:
<PublishedPages>
<PublishedPage>
<Action>Published</Action>
<PageGuid>.....</PageGuid>
<SearchableProperties>
<Name>Name 1</Name>
<Name>Name 2</Name>
</SearchablePeoperties>
</PublishedPage>
<PublishedPage>
...
<PublishedPage>
</PublishedPages>
<MovedPages>
...
</MovedPages>
<DeletedPages>
...
</DeletedPages>
任何帮助,将不胜感激。
谢谢!
Any help would be appreciated.
Thank you!
推荐答案
我觉得这些链接将是相当有帮助的。一旦你的代码和结构的认识,那将是非常容易的。请尝试以下
I think these links would be quite helpful. Once you get understanding of the code and structure, it would be very easy. Please try these
http://blog.pankajmishra.in/writing-xml-file-using-xmlwriter/
的 http://www.java2s.com/Code/CSharp/XML/ProgrammaticallycreatinganewXMLdocument.htm
HTTP://www.codeguru .COM / CSHARP / CSHARP / cs_data / XML / article.php / c4227
好运:)
这篇关于如何从对象列表创建XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!