xml namespaces - C# XmlSerializer xmlns in child element matching parent -
i'm using xmlserializer create xml doc in use 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 getting addfixedpriceitemrequest generated serializer contain xmlns bulkdataexchangerequests element has. seems requirement in order work. generate bulk tag using:
writer.writestartelement("bulkdataexchangerequests", "urn:ebay:apis:eblbasecomponents"); i create serializer.
serializer = new xmlserializer(typeof(addfixedpriceitemrequesttype));//, "urn:ebay:apis:eblbasecomponents"); and serialize namespace
request = new addfixedpriceitemrequesttype() { //populate data. }; xmlserializernamespaces namespaces = new xmlserializernamespaces(); namespaces.add("", "urn:ebay:apis:eblbasecomponents"); serializer.serialize(writer, request, namespaces); this type 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 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 out how xmlns of addfixedpriceitemrequest set match bulk xmlns via serializer. or recommend way it. trying avoid writing each property out createelement/writeelement.
that works me
itemtype itemtype = new itemtype() { //populate data. }; apicall apicall = new additemcall() { item = itemtype }; additemrequesttype addfixedpriceitemrequesttype = ((additemcall)apicall).apirequest; xmlserializernamespaces namespaces = new xmlserializernamespaces(new xmlqualifiedname[] { new xmlqualifiedname(string.empty, "urn:ebay:apis:eblbasecomponents")}); xmlserializer xmlserializer = new xmlserializer( addfixedpriceitemrequesttype.gettype(), "urn:ebay:apis:eblbasecomponents"); memorystream memorystream = new memorystream(); xmlserializer.serialize(memorystream, addfixedpriceitemrequesttype , namespaces); memorystream.seek((long)0, seekorigin.begin); xmldocument xmldocument = new xmldocument(); xmldocument.load(memorystream); memorystream.close(); string additemrequesttypexml = xmldocument.outerxml; the output going be:
<additemrequesttype xmlns="urn:ebay:apis:eblbasecomponents"> <item> <applicationdata></applicationdata> <autopay>false</autopay> <categorymappingallowed>true</categorymappingallowed> <country>us</country> <currency>eur</currency> <description> .... <additemrequesttype>
Comments
Post a Comment