C# 如何自定义对象的XML序列化输出?
我有要序列化的可自定义对象类:C# 如何自定义对象的XML序列化输出?,c#,xml,serialization,C#,Xml,Serialization,我有要序列化的可自定义对象类: public partial class CustomObject { public List<CustomProperty> Properties; } public class CustomProperty { public object Value; [XmlAttribute] public string Name; }
public partial class CustomObject
{
public List<CustomProperty> Properties;
}
public class CustomProperty
{
public object Value;
[XmlAttribute]
public string Name;
}
// some class to be used as a value for CustomProperty
public class Person
{
public string Name;
public string Surname;
public string Photo;
[XmlAttribute]
public int Age;
}
公共部分类CustomObject
{
公共财产清单;
}
公共类自定义属性
{
公共客体价值;
[XmlAttribute]
公共字符串名称;
}
//要用作CustomProperty值的某个类
公共阶层人士
{
公共字符串名称;
公共字符串姓氏;
公众弦乐照片;
[XmlAttribute]
公共信息;
}
当前XML序列化输出如下所示:
<CustomObject>
<Properties>
<CustomProperty Name="Employer">
<Value p6:type="Person" xmlns:p6="http://www.w3.org/2001/XMLSchema-instance" Age="30">
<Name>John</Name>
<Surname>Doe</Surname>
<Photo>photos/John.jpg</Photo>
</Value>
</CustomProperty>
<CustomProperty Name="Desc">
<Value xmlns:q1="http://www.w3.org/2001/XMLSchema" p7:type="q1:string" xmlns:p7="http://www.w3.org/2001/XMLSchema-instance">some text</Value>
</CustomProperty>
</Properties>
</CustomObject>
<CustomObject>
<Properties>
<CustomProperty Name="Employer">
<Person Age="30">
<Name>John</Name>
<Surname>Doe</Surname>
<Photo>photos/John.jpg</Photo>
</Person>
</CustomProperty>
<CustomProperty Name="Desc">
<string>some text</string>
</CustomProperty>
</Properties>
</CustomObject>
约翰
雌鹿
照片/John.jpg
一些文本
首先也是最重要的是,我想删除名称空间和所有这些杂音
最终结果应如下所示:
<CustomObject>
<Properties>
<CustomProperty Name="Employer">
<Value p6:type="Person" xmlns:p6="http://www.w3.org/2001/XMLSchema-instance" Age="30">
<Name>John</Name>
<Surname>Doe</Surname>
<Photo>photos/John.jpg</Photo>
</Value>
</CustomProperty>
<CustomProperty Name="Desc">
<Value xmlns:q1="http://www.w3.org/2001/XMLSchema" p7:type="q1:string" xmlns:p7="http://www.w3.org/2001/XMLSchema-instance">some text</Value>
</CustomProperty>
</Properties>
</CustomObject>
<CustomObject>
<Properties>
<CustomProperty Name="Employer">
<Person Age="30">
<Name>John</Name>
<Surname>Doe</Surname>
<Photo>photos/John.jpg</Photo>
</Person>
</CustomProperty>
<CustomProperty Name="Desc">
<string>some text</string>
</CustomProperty>
</Properties>
</CustomObject>
约翰
雌鹿
照片/John.jpg
一些文本
或者这个:
<CustomObject>
<Properties>
<Person Name="Employer" Age="30">
<Name>John</Name>
<Surname>Doe</Surname>
<Photo>photos/John.jpg</Photo>
</Person>
<string Name="Desc">
some text
</string>
</Properties>
</CustomObject>
约翰
雌鹿
照片/John.jpg
一些文本
如何使XmlSerializer这样输出它?查看属性-这至少可以部分解决您的问题。从MSDN:
public class Things {
[XmlElement(DataType = typeof(string)),
XmlElement(DataType = typeof(int))]
public object[] StringsAndInts;
}
将产生
<Things>
<string>Hello</string>
<int>999</int>
<string>World</string>
</Things>
你好
999
世界
查看属性-这至少可以部分解决您的问题。从MSDN:
public class Things {
[XmlElement(DataType = typeof(string)),
XmlElement(DataType = typeof(int))]
public object[] StringsAndInts;
}
将产生
<Things>
<string>Hello</string>
<int>999</int>
<string>World</string>
</Things>
你好
999
世界
您还可以指定元素名称,以确保正确处理类型:
[System.Xml.Serialization.XmlElementAttribute("files", typeof(Files))]
[System.Xml.Serialization.XmlElementAttribute("metrics", typeof(Metrics))]
public object[] Items { get; set; }
还可以指定元素名称以确保正确处理类型:
[System.Xml.Serialization.XmlElementAttribute("files", typeof(Files))]
[System.Xml.Serialization.XmlElementAttribute("metrics", typeof(Metrics))]
public object[] Items { get; set; }
您可以像这样删除命名空间
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(WSOpenShipments), myns);
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, "");
serializer.Serialize(writer, OS, ns);
xmlString = sb.ToString();
使用这个--ns.Add(string.Empty,“”) 您可以删除命名空间,如
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(WSOpenShipments), myns);
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, "");
serializer.Serialize(writer, OS, ns);
xmlString = sb.ToString();
使用这个--ns.Add(string.Empty,“”) 这将获得第一个所需的案例。谢谢这将获得第一个所需的案例。谢谢看起来MSDN在示例中实际有一个错误,我认为它应该是[xmlement(Type=typeof(string))][xmlement(Type=typeof(int))],因为数据类型是string。但这也行!谢谢看起来MSDN在示例中实际有一个错误,我认为它应该是[xmlement(Type=typeof(string))][xmlement(Type=typeof(int))],因为数据类型是string。但这也行!谢谢