Trying to create a child element in Xml serialization, doesn't show up



I am trying to generate the following XML, and this is also the first time that I ever serialize XML. Could anyone explain to me why my <issuer> element isn't showing up?


What is generated:



<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" />


What I need generated:



<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">http://ift.tt/1yAVbNS;
</samlp:Response>


What I don't understand is that I marked the Issuer as an XMLElement.


Here is my code.


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 = "http://ift.tt/1nijPjR";

[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 = "http://ift.tt/1yAVetb";

[XmlAttribute(Namespace = "xmlns", AttributeName = "saml")]
public const string saml = "urn:oasis:names:tc:SAML:2.0:assertion";
}


Lastly, The methods I am trying to use to generate the SAML (and pardon the ugly string manipulation there - I am planning to bomb it)



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();
}
}
}


I know what I am missing must either be something stupid or something small.


Thanks in Advance :)


No comments:

Post a Comment