3

Question: How does the class for the serialization of this XML content look ?

<?xml version="1.0" encoding="utf-8"?>
<vcc:CreateTextSearchResponse xmlns:vcc="urn:veloconnect:catalog-1.1" xmlns:vct="urn:veloconnect:transaction-1.0">
  <vct:BuyersID>12345</vct:BuyersID>
  <vct:ResponseCode>200</vct:ResponseCode>
  <vct:TransactionID>225</vct:TransactionID>
  <vct:StatusCode>2</vct:StatusCode>
  <vct:IsTest>false</vct:IsTest>
  <vcc:TotalCount>3876</vcc:TotalCount>
</vcc:CreateTextSearchResponse>

If I let it run through xsd.exe, it generates an error.

I have no problem generating this:

<?xml version="1.0" encoding="utf-8"?>
<CreateTextSearchResponse>
  <BuyersID>15942</BuyersID>
  <ResponseCode>200</ResponseCode>
  <TransactionID>225</TransactionID>
  <StatusCode>2</StatusCode>
  <IsTest>false</IsTest>
  <TotalCount>3876</TotalCount>
</CreateTextSearchResponse>

It's just that I need those namespaces to deserialize it (and later on reserialize), so I can't just leave it like this (it's needed by a 3rd party web-service)...

2
  • 3
    "it generates an error." is the worst thing to say in a question. Tell us what exactly it says. Commented Jul 14, 2011 at 11:53
  • It says an awful lot, none of it makes any sense at all. Commented Jul 16, 2011 at 7:45

2 Answers 2

5

Like this:

[XmlRoot(Namespace = CreateTextSearchResponse.CatalogNamespace)]
public class CreateTextSearchResponse
{
    public const string CatalogNamespace = "urn:veloconnect:catalog-1.1",
                TransactionNamespace = "urn:veloconnect:transaction-1.0";
    [XmlElement(Namespace=TransactionNamespace)]
    public int BuyersId { get; set; }
    [XmlElement(Namespace = TransactionNamespace)]
    public int ResponseCode { get; set; }
    [XmlElement(Namespace = TransactionNamespace)]
    public int TransactionID { get; set; }
    [XmlElement(Namespace = TransactionNamespace)]
    public int StatusCode { get; set; }
    [XmlElement(Namespace = TransactionNamespace)]
    public bool IsTest { get; set; }
    [XmlElement(Namespace = CatalogNamespace)]
    public int TotalCount { get; set; }
}

public static void Main()
{
    var ser = new XmlSerializer(typeof(CreateTextSearchResponse));
    var obj = new CreateTextSearchResponse
    {
        BuyersId = 12345,
        ResponseCode = 200,
        TransactionID = 225,
        StatusCode = 2,
        IsTest = false,
        TotalCount = 3876
    };
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("vcc", CreateTextSearchResponse.CatalogNamespace);
    ns.Add("vct", CreateTextSearchResponse.TransactionNamespace);
    ser.Serialize(Console.Out, obj, ns);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I see, now I finally understand what the first parameter of ns.Add is for.
-1

unfortunately there are special characters in your namesapce that Xml can't handle

2 Comments

sorry. on second glance I can't see any special characters in the namespace so the xml appears to be valid
No, there aren't. This xml is fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.