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

本文关键字:元素 C#XmlSerializer xmlns | 更新日期: 2023-09-27 18:30:14

我正在使用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写出每个属性。

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

这对我有效


 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>

问题很可能存在于XmlTypeAttribute中——似乎您将类用作顶级容器——应该使用XmlRootAttribute。通过这种方式,您可以将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>

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

在你的情况下,这可能会对易趣有用:

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