子元素匹配父级中的C#XmlSerializer xmlns

时间:2013-09-04 18:39:26

标签: c# xml-namespaces xmlserializer ebay-lms

我正在使用XmlSerializer创建一个用于ebay大型商家服务的xml文档。

<?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>

我遇到的问题是让序列化程序生成的AddFixedPriceItemRequest实际包含像BulkDataExchangeRequests元素那样的xmlns。这似乎是一个要求,以使其工作。我使用以下方法生成批量标记:

writer.WriteStartElement("BulkDataExchangeRequests", "urn:ebay:apis:eBLBaseComponents");

我创建了一个序列化程序。

serializer = new XmlSerializer(typeof(AddFixedPriceItemRequestType));//, "urn:ebay:apis:eBLBaseComponents");

并使用命名空间序列化

request = new AddFixedPriceItemRequestType()
{
//populate data.
};

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("", "urn:ebay:apis:eBLBaseComponents");

serializer.Serialize(writer, request, namespaces);

这是具有xml属性的类型:

[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
}

我的输出结果如下:

<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">

有人可以帮忙解决如何设置addfixedpriceitemrequest的xmlns以通过序列化程序匹配批量xmlns。或者推荐另一种方法。我试图避免使用createelement / writeelement写出每个属性。

3 个答案:

答案 0 :(得分:1)

这对我有用


 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;

输出将是:


 <AddItemRequestType xmlns="urn:ebay:apis:eBLBaseComponents">
  <Item>
    <ApplicationData></ApplicationData>
    <AutoPay>false</AutoPay>
    <CategoryMappingAllowed>true</CategoryMappingAllowed>
    <Country>US</Country>
    <Currency>EUR</Currency>
    <Description>
    ....

 <AddItemRequestType>

答案 1 :(得分:0)

问题最有可能在于XmlTypeAttribute - 您似乎将自己的类用作顶级容器 - 您应该使用XmlRootAttribute insted。这样,您就可以将urn:ebay:apis:eBLBaseComponents放置在正确的位置。

样品:

void Main()
{
    var t = new A {FieldOfB = new B {C = 5}};

    var serializer = new XmlSerializer(typeof(A));


    using (var writer = new StringWriter())
    {
        serializer.Serialize(writer, t);
        writer.ToString().Dump();
    }
}

与此类定义一起使用时:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:ebay:apis:eBLBaseComponents")]
public class A
{
    public B FieldOfB {get;set;}
}

public class B
{
    public int C {get;set;}
}

得出这个结果:

<?xml version="1.0" encoding="utf-16"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FieldOfB xmlns="urn:ebay:apis:eBLBaseComponents">
    <C>5</C>
  </FieldOfB>
</A>

如果你修复了属性:

[System.Xml.Serialization.XmlRootAttribute(Namespace="urn:ebay:apis:eBLBaseComponents")]
public class A
{
    public B FieldOfB {get;set;}
}

public class B
{
    public int C {get;set;}
}

你明白了:

<?xml version="1.0" encoding="utf-16"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:ebay:apis:eBLBaseComponents">
  <FieldOfB>
    <C>5</C>
  </FieldOfB>
</A>

答案 2 :(得分:0)

eBay仅需要在Trade API中设置名称空间。一种解决方案是删除BulkDataExchange对象中的命名空间设置,并仅在Trade API对象中添加命名空间设置。这不是标准的XML格式,但eBay只接受这一点。

在你的情况下,这可能会成为eBay的伎俩:

writer.WriteStartElement("BulkDataExchangeRequests", "");