1

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>
Djiber
  • 55
  • 1
  • 9
  • The XML you are getting (and expecting) is not well-formed: the prefix `ns2:` on the element `` is not bound to a namespace. Upload it to https://www.xmlvalidation.com/ and you will see the error. But I doubt your framework is actually generating malformed XML, probably something was left out of the question. Can you share the XML you are actually getting, and actually want? – dbc Jul 24 '19 at 09:38
  • I tried test your code here: https://dotnetfiddle.net/SPkaQ1. It doesn't compile because *The type or namespace name `BazaNS2PorukeXMLPHOBS<>` could not be found*. Might you please [edit] your question to share a full [mcve] that we can test? – dbc Jul 24 '19 at 09:41
  • @dbc decided to put simpler example which should compile and run, thanks for pointing that out. – Djiber Jul 24 '19 at 10:50

0 Answers0