I need to implement calls to some service that provider refuses and/or is unable to give WSDL for...
I have trouble with Serializing to XML.
URL in XmlTypeAttribute.TypeName gets encoded.
Since I got some XML examples from provider, I've tried generating classes with Edit=>Paste Special=>Paste XML As Classes, but it simply ignores "type".
I've created appropriate classes manually and everything works except the URL part.
I've checked: C# Escaping XmlType Name with special char within XmlType() Attribute since it's quite similar, but my problem isn't in missing Namespace.
This is what I have for now:
[XmlRoot(Namespace = @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class Book
{
public string Title;
public string Author;
}
[XmlType(TypeName = @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText", Namespace = @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class MyBookType : Book { }
[XmlInclude(typeof(MyBookType))]
[XmlRoot("Books", Namespace = @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class Books : List<Book> { }
public void Main()
{
var b = new Books();
b.Add(new MyBookType
{
Title = "The Art of War",
Author = "Sun Tzu"
});
b.Add(new MyBookType
{
Title = "Great Expectations",
Author = "Charles Dickens"
});
var s = new XmlSerializer(typeof(Books));
s.Serialize(Console.Out, b);
}
XML which I'm expecting:
<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<Book xsi:type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">
<Title>The Art of War</Title>
<Author>Sun Tzu</Author>
</Book>
<Book xsi:type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">
<Title>Great Expectations</Title>
<Author>Charles Dickens</Author>
</Book>
</Books>
XML which I'm getting:
<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<Book xsi:type="http_x003A__x002F__x002F_docs.oasis-open.org_x002F_wss_x002F_2004_x002F_01_x002F_oasis-200401-wss-username-token-profile-1.0_x0023_PasswordText">
<Title>The Art of War</Title>
<Author>Sun Tzu</Author>
</Book>
<Book xsi:type="http_x003A__x002F__x002F_docs.oasis-open.org_x002F_wss_x002F_2004_x002F_01_x002F_oasis-200401-wss-username-token-profile-1.0_x0023_PasswordText">
<Title>Great Expectations</Title>
<Author>Charles Dickens</Author>
</Book>
</Books>