代码如下所示:
StringBuilder builder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using (XmlWriter xmlWriter = XmlWriter.Create(builder, settings))
{
XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
s.Serialize(xmlWriter, objectToSerialize);
}
Run Code Online (Sandbox Code Playgroud)
生成的序列化文档包含名称空间,如下所示:
<message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns="urn:something">
...
</message>
Run Code Online (Sandbox Code Playgroud)
要删除xsi和xsd命名空间,我可以按照如何将对象序列化为XML而不获取xmlns ="..."的答案?.
我希望我的消息标记为<message>(没有任何命名空间属性).我怎样才能做到这一点?
有没有办法配置XmlSerializer,以便它不会在根元素中写入默认命名空间?
我得到的是这个:
<?xml ...>
<rootelement xmlns:xsi="..." xmlns:xsd="...">
</rootelement>
Run Code Online (Sandbox Code Playgroud)
我想删除两个xmlns声明.
我正在构建一个自托管的WCF服务.我正在构建一个特殊的数据结构,以实现非常灵活的数据传输.到目前为止,我测试我的结构是否可以使用DataContractSerializer进行序列化.这工作正常,我很高兴,但有些东西让我烦恼:
在我的XML输出中有许多重新定义的xmlns属性,例如:
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:b="http://www.w3.org/2001/XMLSchema"
Run Code Online (Sandbox Code Playgroud)
这应该在根元素中更好地定义一次,以便可以简单地优化字节.有没有办法将自定义命名空间信息添加到根元素?
这是一个更大的例子来证明我的意思:
<DataObject xmlns="http://schemas.datacontract.org/2004/07/Test"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Data xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:KeyValueOfstringanyType>
<a:Key>ID</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>Value</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">42</a:Value>
</a:KeyValueOfstringanyType>
</Data>
<Data xmlns:a="...">...</Data>
<Data xmlns:a="...">...</Data>
<Data xmlns:a="...">...</Data>
</DataObject>
Run Code Online (Sandbox Code Playgroud)
我想要的是这样的:
<DataObject xmlns="http://schemas.datacontract.org/2004/07/Test"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:b="http://www.w3.org/2001/XMLSchema">
<Data>
<a:KeyValueOfstringanyType>
<a:Key>ID</a:Key>
<a:Value i:type="b:int">1</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>Value</a:Key>
<a:Value i:type="b:int">42</a:Value>
</a:KeyValueOfstringanyType>
</Data>
<Data>...</Data>
<Data>...</Data>
<Data>...</Data>
</DataObject>
Run Code Online (Sandbox Code Playgroud) 我得到了一个 xsd 生成的 C# POCO 对象,我需要将其转换为 xml。然而,预期的有效载荷与我得到的 xsds 不匹配。具体来说,我需要省略声明并从 xml 对象中删除所有命名空间,以便相关公司接受 API 请求。
问题
给定一个类型为 T 的对象,我想在没有声明和命名空间的情况下对其进行序列化。
我已经摆脱了大部分,但由于某种原因,q1 已添加到每个元素中。我如何删除它?
试图
经过一些研究,我看到一些帖子提供了一个解决方案,该解决方案创建一个空的 xml 序列化器命名空间并使用该对象调用序列化器。那只让我成功了一半。
用法
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
var body = payload.SerializeObject(false, true, ns);
Run Code Online (Sandbox Code Playgroud)
扩展方法
public static string SerializeObject<T>(this T obj, bool indented, bool omitDeclaration, XmlSerializerNamespaces ns)
{
var utf8NoBom = new UTF8Encoding(false);
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = omitDeclaration,
Indent = indented,
Encoding = utf8NoBom
};
using (MemoryStream ms = new MemoryStream())
{
using …Run Code Online (Sandbox Code Playgroud) 我尝试使用自定义命名空间序列化对象.这就是这个类的样子:
[XmlRoot("Root", Namespace = "myNamespace")]
public partial class MyClass
{
public MyClass()
{
this.Xmlns = new XmlSerializerNamespaces();
this.Xmlns.Add(string.Empty, "myNamespace");
}
[XmlNamespaceDeclarations()]
public XmlSerializerNamespaces Xmlns = null;
}
Run Code Online (Sandbox Code Playgroud)
以下是序列化它的代码:
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
serializer.Serialize(xmlWriter, obj);
Run Code Online (Sandbox Code Playgroud)
预期的结果是
<Root xmlns="myNamespace" />
Run Code Online (Sandbox Code Playgroud)
但是它仍然有xmlns:xsi和xmlns:xsd属性:
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="myNamespace" />
Run Code Online (Sandbox Code Playgroud)
当我序列化使用时serializer.Serialize(xmlWriter, obj, obj.Xmlns),结果是正确的.
为什么序列化程序会忽略该XmlNamespaceDeclarations属性?它不应该自动从它获取命名空间声明吗?如何在序列化类中定义名称空间?
提前致谢!