C# XML可以';t将xmlns写入子节点

C# XML可以';t将xmlns写入子节点,c#,xml,xml-namespaces,C#,Xml,Xml Namespaces,嗨,我有3个这样的课程: public abstract class XmlNs { public const string XmlnsAttribute = "urn:ebay:apis:eBLBaseComponents"; } [Serializable] public class BulkDataExchangeRequests : XmlNs { [XmlAttribute("xmlns")] public string XmlNs = XmlnsAttri

嗨,我有3个这样的课程:

public abstract class XmlNs
{
    public const string XmlnsAttribute = "urn:ebay:apis:eBLBaseComponents";
}


[Serializable]
public class BulkDataExchangeRequests : XmlNs
{
    [XmlAttribute("xmlns")]
    public string XmlNs = XmlnsAttribute;
    [XmlElement("Header")]
    public Header Header { get; set; }
    [XmlElement("AddFixedPriceItemRequest")]
    public List<AddFixedPriceItemRequest> ListAddFixedPriceItemRequest { get; set; }
}

[Serializable]
public class AddFixedPriceItemRequest : XmlNs
{
    [XmlElement("ErrorLanguage")]
    public string ErrorLanguage { get; set; }
    [XmlElement("WarningLevel")]
    public string WarningLevel { get; set; }
    [XmlElement("Version")]
    public string Version { get; set; }
    [XmlElement("Item")]
    public ItemType Item { get; set; }
    [XmlAttribute("xmlns")]
    public string XmlNs = XmlnsAttribute;
}
公共抽象类XmlNs
{
public const string XmlnsAttribute=“urn:ebay:api:eblbbasecomponents”;
}
[可序列化]
公共类BulkDataExchangeRequests:XmlNs
{
[xmldattribute(“xmlns”)]
公共字符串XmlNs=XmlnsAttribute;
[XmlElement(“标题”)]
公共标头{get;set;}
[XmlElement(“AddFixedPriceItemRequest”)]
公共列表ListAddFixedPriceItemRequest{get;set;}
}
[可序列化]
公共类AddFixedPriceItemRequest:XmlNs
{
[XmlElement(“ErrorLanguage”)]
公共字符串错误语言{get;set;}
[XmlElement(“警告级别”)]
公共字符串警告级别{get;set;}
[XmlElement(“版本”)]
公共字符串版本{get;set;}
[XmlElement(“项目”)]
公共ItemType项{get;set;}
[xmldattribute(“xmlns”)]
公共字符串XmlNs=XmlnsAttribute;
}
问题是,当我序列化对象时,我得到了一个正确的xml,但AddFixedPriceItemRequest项中没有xmlns属性,而BulkDataExchangeRequests中的xmlns被正确写入


非常感谢您的帮助……

您正在嵌套元素,如果不指定任何其他内容,嵌套元素与其父元素位于同一命名空间中。因此,实际上您的序列化程序不再输出
xmlns
属性是正确的,因为它是不需要的

见:


这也在名称空间“我的名称空间”中,无需进一步声明

那么……如何才能做到这一点呢?美国高中619。。。恩我们高中659。。。这不是正确的序列化程序的输出,正确的解析器也不需要重新声明名称空间。这是不必要的。(这类似于在每句话的开头重新说明以下内容将是英文的。)我可以看到与您相同的源代码(我想),但我找不到任何明确的书面要求。您是否已尝试将这些命令发送到易趣?错误消息是什么(如果有的话)?如果我没有放置名称空间,我会得到一个意外的标记错误…这是一个易趣示例如果您喜欢我的答案,并且它回答了您的问题,如果您能接受它作为答案,我将非常高兴;-)
<root xmlns="my-namespace">
    <element>this is also in the namespace "my-namespace" without further declaration</element>
    <so><are><child><elements></elements></child></are></so>
</root>
[Serializable]
public class BulkDataExchangeRequests : XmlNs
{
    [XmlElement("Header")]
    public Header Header { get; set; }
    [XmlElement("AddFixedPriceItemRequest")]
    public List<AddFixedPriceItemRequest> ListAddFixedPriceItemRequest { get; set; }

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(new System.Xml.XmlQualifiedName[] { new System.Xml.XmlQualifiedName("", XmlnsAttribute) });
}

[Serializable]
public class AddFixedPriceItemRequest : XmlNs
{
    [XmlElement("ErrorLanguage")]
    public string ErrorLanguage { get; set; }
    [XmlElement("WarningLevel")]
    public string WarningLevel { get; set; }
    [XmlElement("Version")]
    public string Version { get; set; }
    [XmlElement("Item")]
    public ItemType Item { get; set; }

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(new System.Xml.XmlQualifiedName[] { new System.Xml.XmlQualifiedName("", XmlnsAttribute) });
}
<?xml version="1.0" encoding="utf-16"?>
<BulkDataExchangeRequests xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:ebay:apis:eBLBaseComponents">
  <AddFixedPriceItemRequest xmlns="urn:ebay:apis:eBLBaseComponents" />
  <AddFixedPriceItemRequest xmlns="urn:ebay:apis:eBLBaseComponents" />
</BulkDataExchangeRequests>