开发者

Serializing XML with strange namespaces?

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 generate开发者_如何学Cs 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)...


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜