I'm using XmlSerializer to create a xml doc in use for ebay's large merchant services.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<BulkDataExchangeRequests xmlns="urn:ebay:apis:eBLBaseComponents">
<Header>
<SiteID>0</SiteID>
<Version>775</Version>
</Header>
<AddFixedPriceItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<Version>775</Version>
<Item>
<AutoPay>false</AutoPay>
<BuyerProtection>ItemIneligible</BuyerProtection>
<BuyItNowPrice currencyID="USD">0.0</BuyItNowPrice>
<Country>US</Country>
<Currency>USD</Currency>
<Description>test</Description>
<GiftIcon>0</GiftIcon>
</Item>
</AddFixedPriceItemRequest>
</BulkDataExchangeRequests>
The problem I'm having is getting the AddFixedPriceItemRequest generated by the serializer to actually contain that xmlns like the BulkDataExchangeRequests element has. It seems to be a requirement in order for this to work. I generate the bulk tag using:
writer.WriteStartElement("BulkDataExchangeRequests", "urn:ebay:apis:eBLBaseComponents");
I create a serializer.
serializer = new XmlSerializer(typeof(AddFixedPriceItemRequestType));//, "urn:ebay:apis:eBLBaseComponents");
and serialize with the namespace
request = new AddFixedPriceItemRequestType()
{
//populate data.
};
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("", "urn:ebay:apis:eBLBaseComponents");
serializer.Serialize(writer, request, namespaces);
This is the type with the xml attributes:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.5420")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:ebay:apis:eBLBaseComponents",TypeName="AddFixedPriceItemRequest")]
public partial class AddFixedPriceItemRequestType : AbstractRequestType {
//filled in class
}
my output ends up like this:
<AddFixedPriceItemRequest xmlns="">
<ErrorLanguage xmlns="urn:ebay:apis:eBLBaseComponents">en_US</ErrorLanguage>
<Version xmlns="urn:ebay:apis:eBLBaseComponents">837</Version>
<Item p4:type="Item" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:ebay:apis:eBLBaseComponents">
Could someone help out with how to get xmlns of addfixedpriceitemrequest set to match the bulk xmlns via the serializer. Or recommend another way to do it. I was trying to avoid writing each property out with createelement/writeelement.
The problem most likely lies in the
XmlTypeAttribute
- it seems you use your class as the top level container - you should useXmlRootAttribute
insted. This way you will geturn:ebay:apis:eBLBaseComponents
placed in correct location.Sample:
When used with this class definitions:
yields this result:
And if you fix the attribute:
You get this: