我正在尝试序列化一个继承了this网站上所见的接口ICollection的类,但是它没有出现在我的XML上。什么可能是错的?为什么我的XMLSerializer包括AssertionAttribute
集合?至于我的例子,我提前为所附的大量编码道歉。
为此元素生成的XML:
<saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema" />
XML预计将从我的示例中生成:
<saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema">
<saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic" Name="Language">
<saml:AttributeValue xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">en-US</saml:AttributeValue>
</saml:Attribute>
<saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic" Name="fromApp">
<saml:AttributeValue xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">SP2</saml:AttributeValue>
</saml:Attribute>
<saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic" Name="Action">
<saml:AttributeValue xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">VSREPORT</saml:AttributeValue>
</saml:Attribute>
</saml:AttributeStatement>
代码:
AttributeStatement.cs
[Serializable]
[XmlType(Namespace="http://www.w3.org/2001/XMLSchema")]
public class AttributeStatement
{
private static AssertionAttributes attributes = new AssertionAttributes();
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
[XmlAttribute(AttributeName = "Attribute")]
public static AssertionAttributes attribute;
public AttributeStatement()
{
xmlns.Add("xs", "http://www.w3.org/2001/XMLSchema");
SetAttributes();
}
private static void SetAttributes()
{
var languageAttribute = new AssertionAttribute("en-US");
languageAttribute.Name = "Name";
attributes.Add(languageAttribute);
var fromAppAttribute = new AssertionAttribute("SP2");
fromAppAttribute.Name = "fromApp";
attributes.Add(fromAppAttribute);
var actionAttribute = new AssertionAttribute("VSREPORT");
actionAttribute.Name = "Action";
attributes.Add(actionAttribute);
attribute = attributes;
}
}
AssertionAttributes.cs - 该类应包含Type AssertionAttribute的集合
[Serializable]
[XmlType(Namespace = "urn:oasis:names:tc:SAML:2.0:assertion")]
public class AssertionAttributes : ICollection
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public AssertionAttributes()
{
xmlns.Add("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
}
private ArrayList empArray = new ArrayList();
public AssertionAttribute this[int index]
{
get { return (AssertionAttribute)empArray[index]; }
}
void ICollection.CopyTo(Array array, int index)
{
empArray.CopyTo(array, index);
}
int ICollection.Count
{
get { return empArray.Count; }
}
bool ICollection.IsSynchronized
{
get { return false; }
}
object ICollection.SyncRoot
{
get { return this; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return empArray.GetEnumerator();
}
public void Add(AssertionAttribute assertionAttribute)
{
empArray.Add(assertionAttribute);
}
}
AssertionAttribute.cs - 应该在xml中显示为多个标签(生成)
[Serializable]
[XmlType(Namespace = "urn:oasis:names:tc:SAML:2.0:assertion")]
public class AssertionAttribute
{
[XmlAttribute(AttributeName = "NameFormat")]
public string NameFormat = "urn:oasis:names:tc:SAML:2.0:attrname-format:basic";
[XmlElement(ElementName = "AttributeValue")]
public AttributeValue attributeValue;
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public AssertionAttribute(string attributeValueText)
{
xmlns.Add("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
AttributeValueText = attributeValueText;
attributeValue = new AttributeValue(attributeValueText);
}
private string name;
[XmlAttribute(AttributeName = "Name")]
public string Name
{
get { return name; }
set { name = value; }
}
private static string attributeValueText;
public static string AttributeValueText
{
get { return attributeValueText; }
set { attributeValueText = value; }
}
}
AttributeValue.cs - 仅用于存储此元素的文本值的XML值
[Serializable]
[XmlType(Namespace="urn:oasis:names:tc:SAML:2.0:assertion")]
public class AttributeValue
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public AttributeValue(string attributeValueText)
{
xmlns.Add("saml", "urn.oasis:names:tc:SAML:2.0:assertion");
Text = attributeValueText;
}
private string text;
[XmlText]
public string Text
{
get { return text; }
set { text = value; }
}
}
答案 0 :(得分:1)
默认情况下,在XML序列化期间,只会序列化对象的公共属性和字段。您的AssertionAttribute
是公共字段,因此应该序列化,对吧?实际上,不:注意单词object
:)。由于AssertionAttribute
是static
,因此它是该类的字段,而不是该类的对象。如果您可以安排诸如删除static
修饰符之类的内容,那么应该这样做。