Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在Xml序列化中创建子元素,不会';我没有出现_C#_Xml_Serialization - Fatal编程技术网

C# 尝试在Xml序列化中创建子元素,不会';我没有出现

C# 尝试在Xml序列化中创建子元素,不会';我没有出现,c#,xml,serialization,C#,Xml,Serialization,我正在尝试生成以下XML,这也是我第一次序列化XML。有人能解释一下为什么我的元素没有出现吗 生成的内容: <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:proto

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

生成的内容:

<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> 
Issuer.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();

}
[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(请原谅这里丑陋的字符串操作——我正计划轰炸它)

我知道我错过的一定是一些愚蠢的东西或是一些小东西


提前感谢:)

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

下面是一个成功的例子:

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的输出遗漏了“其他”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>


明白了。谢谢只是在节目中出现了随机名称空间的问题,但这是另一个问题day@a-h、 我认为这只是因为
只读
关键字,而不是属性与字段。
<?xml version="1.0"?>
<ToSerialize2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Other />
</ToSerialize2>