【发布时间】:2019-03-27 12:01:25
【问题描述】:
我正在尝试将一个对象序列化为一个字符串。
从中获取 c# 模型的 xml 有多个命名空间:
xmlns="http://www.example.org/standards/def/1"
xmlns:ac="http://www.example.org/Standards/xyz/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rlc="http://www.example.org/standards/def/1"
xmlns:def1="http://www.lol.com/Standards/lol.xsd" Version="2013-06" xsi:schemaLocation="http://www.lol.org/standards/def/1 lol.xsd"
我正在序列化它:
var deserialize = (MyType)pageDeserializer.Deserialize(reader);
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("ac", "urn:http://www.example.org/Standards/xyz/1");
namespaces.Add("rlc", "urn:http://www.example.org/standards/def/1");
namespaces.Add("def1", "http://www.lol.com/Standards/lol.xsd" Version="2013-06" xsi:schemaLocation="http://www.lol.org/standards/def/1 lol.xsd");
var str = pageDeserializer.SerializeAsUtf8<JvInsReinsurance>(deserialize, namespaces);
方法SerializeAsUtf8在哪里:
public static string SerializeAsUtf8<T>(this XmlSerializer serializer, T o, XmlSerializerNamespaces ns)
{
using (var textWriter = new Utf8StringWriter())
{
serializer.Serialize(textWriter, o, ns);
return textWriter.ToString();
}
}
我希望我的 XML 看起来像:
<rlc:element1 attribute1="value">
<ac:element1>VALUR</ac:element1>
</rlc:element1>
我得到的是:
<element1 attribute1="value">
<element1>VALUR</element1>
</element1>
但是没有包含命名空间的信息,这使得后续的 xsd 验证失败。如何获取包含的命名空间前缀?
更新 1
按照 cmets 中的建议移除骨灰盒,让我迈出了第一步。现在我在验证 XSD 时遇到错误。
我收到以下错误:
1.
The element 'ElementX' in namespace 'urn:http://www.example.org/standards/def/1' has invalid child element 'ElementY' in namespace 'http://www.example.org/standards/def/1'.
2.
The element 'ElementP' in namespace 'urn:http://www.example.org/standards/def/1' has invalid child element 'ElementQ' in namespace 'http://www.example.org/standards/def/1'.
对于 1. 类是
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public partial class ElementX
{
[XmlElement("ElementYName")]
public ElementY[] ElementYNames { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public partial class ElementY
{
[XmlAttribute]
public string Field1 { get; set; }
public ElementYFieldAmountType FieldAmount { get; set; }
public string Field2 { get; set; }
private string field3;
/// <remarks/>
public string Field3
{
get
{
return this.field3;
}
set
{
this.field3 = value;
}
}
}
[Serializable]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class ElementYFieldAmountType
{
public FieldAmount Amt { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class FieldAmount
{
private string _ccyField;
private decimal valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Ccy
{
get
{
return this._ccyField;
}
set
{
this._ccyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public decimal Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
使用 XSD
<xs:complexType name="ElementX">
<xs:sequence>
<xs:element ref="ElementY" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="ElementY" type="ElementYType"/>
<xs:element name="FieldAmount" type="AnyAmtType"/>
<xs:complexType name="ElementYType">
<xs:sequence>
<xs:element ref="Field2" minOccurs="0"/>
<xs:element ref="FieldAmount" minOccurs="0"/>
<xs:element ref="Field3" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="Field1" type="xs:NMTOKEN" use="required"/>
</xs:complexType>
2人份
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public partial class ElementP
{
public ElementQ ElementQName { get; set; }
}
[Serializable]
[DesignerCategory("code")]
[XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class ElementQ
{
public PercentageRateType Rate { get; set; }
}
[Serializable]
[DesignerCategory("code")]
[XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class PercentageRateType
{
[XmlAttribute]
public string RateUnit { get; set; }
[XmlText]
public decimal Value { get; set; }
}
它们在我看来很好,它们有什么问题?
【问题讨论】:
-
为什么你使用 urn:http:// 而不是 http:// ?
-
好问题。这是我无法控制的,我们正在使用该定义附带的现有消息
-
你试过使用
XmlNamespaceManager吗? Here 是一个小例子 -
我假设它的工作方式与 XmlSerializerNamespaces 参数的工作方式相同。另外,如果我错了,请纠正,但在示例中代码是反序列化字符串?
-
你的班级是什么样的?每个类的命名空间必须在类上方方括号中的属性中。
标签: c# xml serialization