C# 如何删除<;ArrayOfX>;序列化列表时的容器元素<;T>;转换为现有的XML文档
我有以下课程:C# 如何删除<;ArrayOfX>;序列化列表时的容器元素<;T>;转换为现有的XML文档,c#,asp.net,xml,serialization,linq-to-xml,C#,Asp.net,Xml,Serialization,Linq To Xml,我有以下课程: public class WikiEntry { public string Id { get; set; } public string Title { get; set; } public string Content { get; set; } [XmlArray] public List<Category> Categories { get; set; } } public class Category { [
public class WikiEntry
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
[XmlArray]
public List<Category> Categories { get; set; }
}
public class Category
{
[XmlAttribute]
public string Id { get; set; }
[XmlAttribute]
public string Text { get; set; }
}
问题是,当我这样做时,我会得到:
<Categories>
<ArrayOfCategory>
<Category Id="482ce9f6-5d4c-48f9-b84f-33c3cf9b0b0f" Text="CATEGORYA" />
<Category Id="73e6c671-fb6d-40a4-8694-1d5dbcf381d5" Text="CATEGORYB" />
</ArrayOfCategory>
<ArrayOfCategory>
<Category Id="3c0f2a15-4623-4f33-b356-75e8c8b89624" Text="CATEGORYA" />
<Category Id="d8720ca9-06f5-401d-90e2-c7f43e1c91f5" Text="CATEGORYB" />
</ArrayOfCategory>
因此,我的问题是如何序列化我的Category类,以便得到以下结果(省略
父类):
注意:我想删除它,而不是重命名它。将根名称作为参数传递给
XMLSerializer
调用可以做到这一点
XmlSerializer serializer=newxmlserializer(typeof(List),newxmlrootattribute(“RootElementName”)代码>以下是一些Linq to Xml,以实现您的目标:
categories.Add(XElement.Parse(categoriesBuilder.ToString().Trim()));
XDocument output =
new XDocument(
new XElement(xDoc.Root.Name,
new XElement("Categories",
from comp in xDoc.Root.Elements("Categories").Elements("ArrayOfCategory").Elements("Category")
select new XElement("Category",
new XAttribute("Id", comp.Attribute("Id").Value),
new XAttribute("Text", comp.Attribute("Text").Value)
))));
output.Save("c:\\so\\test.xml");
您可以使用直接序列化到XDocument
。这反过来将允许您直接序列化为类别
元素的子元素
,而无需任何中间字符串表示
首先,定义以下扩展方法:
public static class XObjectExtensions
{
public static XElement SerializeToXElement<T>(this T obj, XContainer parent = null, XmlSerializer serializer = null, XmlSerializerNamespaces ns = null)
{
if (obj == null)
throw new ArgumentNullException();
// Initially, write to a fresh XDocument to cleanly avoid the exception described in
// https://stackoverflow.com/questions/19045921/net-xmlserialize-throws-writestartdocument-cannot-be-called-on-writers-created
var doc = new XDocument();
using (var writer = doc.CreateWriter())
{
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(writer, obj, ns ?? NoStandardXmlNamespaces());
}
// Now move to the incoming parent.
var element = doc.Root;
if (element != null)
{
element.Remove();
if (parent != null)
{
parent.Add(element);
}
}
return element;
}
public static XmlSerializerNamespaces NoStandardXmlNamespaces()
{
var ns = new XmlSerializerNamespaces();
ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
return ns;
}
}
工作示例.Net fiddle。您能发布完整的代码,包括如何实例化categoriesBuilder和Wiki吗?您忘了显示xDoc是如何实例化的。完成。感谢您捕捉到这一点。添加了响应作为答案。您在寻找吗?很聪明,但不是,它只是使内部元素与外部父节点同名,解决方案是否满足了您的需要?我已经使用了LINQ to XML,但不是我要找的。不过,谢谢你的建议!没问题,尽管很难想象为什么这对你不起作用。下一次,如果你在回避一个解决方案,那么你的问题就要具体点,这样这里的人就不会浪费时间来帮助你做你不需要的事情。我不是在“回避”一个解决方案。我对@dbc足够具体,可以帮助我解决问题。
<Categories>
<Category Id="482ce9f6-5d4c-48f9-b84f-33c3cf9b0b0f" Text="CATEGORYA" />
<Category Id="73e6c671-fb6d-40a4-8694-1d5dbcf381d5" Text="CATEGORYB" />
<Category Id="3c0f2a15-4623-4f33-b356-75e8c8b89624" Text="CATEGORYA" />
<Category Id="d8720ca9-06f5-401d-90e2-c7f43e1c91f5" Text="CATEGORYB" />
</Categories>
categories.Add(XElement.Parse(categoriesBuilder.ToString().Trim()));
XDocument output =
new XDocument(
new XElement(xDoc.Root.Name,
new XElement("Categories",
from comp in xDoc.Root.Elements("Categories").Elements("ArrayOfCategory").Elements("Category")
select new XElement("Category",
new XAttribute("Id", comp.Attribute("Id").Value),
new XAttribute("Text", comp.Attribute("Text").Value)
))));
output.Save("c:\\so\\test.xml");
public static class XObjectExtensions
{
public static XElement SerializeToXElement<T>(this T obj, XContainer parent = null, XmlSerializer serializer = null, XmlSerializerNamespaces ns = null)
{
if (obj == null)
throw new ArgumentNullException();
// Initially, write to a fresh XDocument to cleanly avoid the exception described in
// https://stackoverflow.com/questions/19045921/net-xmlserialize-throws-writestartdocument-cannot-be-called-on-writers-created
var doc = new XDocument();
using (var writer = doc.CreateWriter())
{
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(writer, obj, ns ?? NoStandardXmlNamespaces());
}
// Now move to the incoming parent.
var element = doc.Root;
if (element != null)
{
element.Remove();
if (parent != null)
{
parent.Add(element);
}
}
return element;
}
public static XmlSerializerNamespaces NoStandardXmlNamespaces()
{
var ns = new XmlSerializerNamespaces();
ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
return ns;
}
}
var categories = xDoc.Root.Element("Categories");
foreach (var category in wiki.Categories)
{
category.SerializeToXElement(categories);
}