尝试在Xml序列化中创建子元素,没有显示

本文关键字:元素 显示 创建 Xml 序列化 | 更新日期: 2023-09-27 18:13:33

我正在尝试生成以下XML,这也是我第一次序列化XML。有人能解释一下为什么我的<issuer>元素没有显示出来吗?

生成内容:

<samlp:Response xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="02279359-0581-41c7-a66b-199523ac8eab" IssueInstant="18:07:2014 10:41:37 AM" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" /> 

我需要生成的内容:

<samlp:Response ID="02279359-0581-41c7-a66b-199523ac8eab" IssueInstant="18:07:2014 10:41:37 AM" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" >
    <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">https://www.partner.com/sso</saml:Issuer>
</samlp:Response> 

我不明白的是,我将发行人标记为XMLElement。

这是我的代码。

MySaml.cs

[Serializable]
[XmlRoot(ElementName = "Response", Namespace = "urn:oasis:names:tc:SAML:2.0:protocol", IsNullable = false)]
public class MySaml
{
    [XmlAttribute("ID")]
    public string ID { get; set; }
    [XmlAttribute("Version")]
    public const string Version = "2.0";
    [XmlAttribute("IssueInstant")]
    public string IssueInstant { get; set; }
    [XmlAttribute("Destination")]
    public const string Destination = "https://www.site.com/SC/SSO/SingleSignOn.aspx";
    [XmlAttribute(Namespace = "xmlns", AttributeName = "samlp")]
    public const string samlp = "urn:oasis:names:tc:SAML:2.0:protocol";
    [XmlElement(Namespace = "urn:oasis:names:tc:SAML:2.0:assertion", ElementName = "Issuer", IsNullable = false)]
    public readonly Issuer Issuer = new Issuer();
}

Issuer.cs

[Serializable]
public class Issuer
{
    [XmlText]
    public const string Text = "https://www.partner.com/sso";
    [XmlAttribute(Namespace = "xmlns", AttributeName = "saml")]
    public const string saml = "urn:oasis:names:tc:SAML:2.0:assertion";
}

最后,我试图用来生成SAML的方法(请原谅那里丑陋的字符串操作-我计划轰炸它)

protected void Page_Load(object sender, EventArgs e)
{
    GenerateSamlAssertion();  
}
private void GenerateSamlAssertion()
{
    var response = new MySaml();
    response.ID = Guid.NewGuid().ToString();
    response.IssueInstant = DateTime.UtcNow.ToString("dd:MM:yyyy hh:mm:ss tt");
    SerializeXml(response);
}
public XmlDocument SerializeXml(MySaml mySaml)
{
    var xmlSerializerNameSpace = new XmlSerializerNamespaces();
    xmlSerializerNameSpace.Add("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
    xmlSerializerNameSpace.Add("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
    var serializer = new XmlSerializer(typeof(MySaml));
    using (var writer = new StringWriter())
    {
        try
        {
            serializer.Serialize(writer, mySaml, xmlSerializerNameSpace);             
            var doc = new XmlDocument();
            doc.LoadXml(writer.ToString().Remove(0, writer.ToString().IndexOf("'r'n") + 1));
            return doc;
        }
        finally
        {
            writer.Close();
        }
    }
}

我知道我错过的要么是愚蠢的东西,要么是微不足道的东西。

提前感谢:)

尝试在Xml序列化中创建子元素,没有显示

为了序列化,字段或属性必须是公共的,不能是只读的。

下面是一个工作示例:

void Main()
{
    Serialize(new ToSerialize());
    Serialize(new ToSerialize2());
}
private void Serialize(object o)
{
    var serializer = new XmlSerializer(o.GetType());
    var ms = new MemoryStream();
    serializer.Serialize(ms, o);
    Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
}
public class ToSerialize2
{
    public ToSerialize2()
    {
        this.Other = new Other();
    }
    public Other Other;
}
public class ToSerialize
{
    public readonly Other Other = new Other();
}
public class Other
{
}

ToSerialzie的输出漏掉了"Other"XML元素。

<?xml version="1.0"?>
<ToSerialize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

ToSerialize2包含它:

<?xml version="1.0"?>
<ToSerialize2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Other />
</ToSerialize2>