如何让XMLSerializer将命名空间添加到嵌套对象中的属性?(How do I get the XMLSerializer to add namespaces to attributes in nested objects?)

这是我得到的:

<ex:test soap:mustUnderstand="1" xmlns:ex="http://www.example.com/namespace">
  <ex:A Type="lorem">ipsum</ex:A>
</ex:test>

这是我想要的:(请注意,Type-attribute前缀为ex。)

<ex:test soap:mustUnderstand="1" xmlns:ex="http://www.example.com/namespace">
  <ex:A ex:Type="lorem">ipsum</ex:A>
</ex:test>

这是我的代码:

  [XmlType(Namespace = "http://www.example.com/namespace")]
  [XmlRoot("ex", Namespace = "http://www.example.com/namespace")]
  public class TestSoapHeader : SoapHeader {
    private TestSoapHeaderTypeValuePair _a;

    public TestHeader() {
      MustUnderstand = true;
    }

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlsn {
      get {
        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
        xsn.Add("ex", "http://www.example.com/namespace");
        return xsn;
      }
      set { }
    }

    public TestSoapHeaderTypeValuePair A {
      get { return _a; }
      set { _a = value; }
    }

  }

  [XmlType(Namespace = "http://www.example.com/namespace")]
  public class TestSoapHeaderTypeValuePair {
    private string _type;
    private string _value;

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlsn
    {
      get
      {
        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
        xsn.Add("ex", "http://www.example.com/namespace");
        return xsn;
      }
      set { }
    }

    public TestSoapHeaderTypeValuePair(string type, string value) {
      Type = type;
      Value = value;
    }

    public TestSoapHeaderTypeValuePair() {}

    [System.Xml.Serialization.XmlAttributeAttribute("type", Namespace = "http://www.example.com/namespace")]
    public string Type {
      get { return _type; }
      set { _type = value; }
    }

    [System.Xml.Serialization.XmlText()]
    public string Value {
      get { return _value; }
      set { _value = value; }
    }
  }

This is what I get:

<ex:test soap:mustUnderstand="1" xmlns:ex="http://www.example.com/namespace">
  <ex:A Type="lorem">ipsum</ex:A>
</ex:test>

This is what I want: (Note that the Type-attribute is prefixed with ex.)

<ex:test soap:mustUnderstand="1" xmlns:ex="http://www.example.com/namespace">
  <ex:A ex:Type="lorem">ipsum</ex:A>
</ex:test>

This is my code:

  [XmlType(Namespace = "http://www.example.com/namespace")]
  [XmlRoot("ex", Namespace = "http://www.example.com/namespace")]
  public class TestSoapHeader : SoapHeader {
    private TestSoapHeaderTypeValuePair _a;

    public TestHeader() {
      MustUnderstand = true;
    }

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlsn {
      get {
        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
        xsn.Add("ex", "http://www.example.com/namespace");
        return xsn;
      }
      set { }
    }

    public TestSoapHeaderTypeValuePair A {
      get { return _a; }
      set { _a = value; }
    }

  }

  [XmlType(Namespace = "http://www.example.com/namespace")]
  public class TestSoapHeaderTypeValuePair {
    private string _type;
    private string _value;

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlsn
    {
      get
      {
        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
        xsn.Add("ex", "http://www.example.com/namespace");
        return xsn;
      }
      set { }
    }

    public TestSoapHeaderTypeValuePair(string type, string value) {
      Type = type;
      Value = value;
    }

    public TestSoapHeaderTypeValuePair() {}

    [System.Xml.Serialization.XmlAttributeAttribute("type", Namespace = "http://www.example.com/namespace")]
    public string Type {
      get { return _type; }
      set { _type = value; }
    }

    [System.Xml.Serialization.XmlText()]
    public string Value {
      get { return _value; }
      set { _value = value; }
    }
  }
2023-03-27 19:03

满意答案

IXmlSerializable也许?

注意我还添加了(对A ):

[XmlElement("A", Namespace = "http://www.example.com/namespace")]
public TestSoapHeaderTypeValuePair A {...}

代码如下:

public class TestSoapHeaderTypeValuePair : IXmlSerializable
{
    private string _type;
    private string _value;

    public TestSoapHeaderTypeValuePair(string type, string value)
    {
        Type = type;
        Value = value;
    }

    public TestSoapHeaderTypeValuePair() { }

    public string Type
    {
        get { return _type; }
        set { _type = value; }
    }

    public string Value
    {
        get { return _value; }
        set { _value = value; }
    }

    #region IXmlSerializable Members

    System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
    {
        return null;
    }

    void IXmlSerializable.ReadXml(System.Xml.XmlReader reader)
    {
        throw new NotImplementedException();
    }

    void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteAttributeString("ex", "type", "http://www.example.com/namespace", Type);
        writer.WriteString(Value);
    }

    #endregion
}

IXmlSerializable maybe?

Note I also added (to A):

[XmlElement("A", Namespace = "http://www.example.com/namespace")]
public TestSoapHeaderTypeValuePair A {...}

here's the code:

public class TestSoapHeaderTypeValuePair : IXmlSerializable
{
    private string _type;
    private string _value;

    public TestSoapHeaderTypeValuePair(string type, string value)
    {
        Type = type;
        Value = value;
    }

    public TestSoapHeaderTypeValuePair() { }

    public string Type
    {
        get { return _type; }
        set { _type = value; }
    }

    public string Value
    {
        get { return _value; }
        set { _value = value; }
    }

    #region IXmlSerializable Members

    System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
    {
        return null;
    }

    void IXmlSerializable.ReadXml(System.Xml.XmlReader reader)
    {
        throw new NotImplementedException();
    }

    void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteAttributeString("ex", "type", "http://www.example.com/namespace", Type);
        writer.WriteString(Value);
    }

    #endregion
}

相关问答

更多

.net XMLSerializer多个命名空间问题(.net XMLSerializer multiple namespace problem)

虽然我现在认为现有的解决方案对我的要求有效,但有一种方法可以实现我的需求。 在要序列化的类中声明XmlSerializerNamespaces对象。 <XmlNamespaceDeclarations()> _ Public xmlns As XmlSerializerNamespaces 然后将名称空间添加到此集合中,而不是直接将其附加到序列化程序。 这具有声明使用它的命名空间所需的效果。 xmlns = New XmlSerializerNamespaces() xm...

在IE中使用JavaScript中的XMLSerializer时,SVG标记中不需要的名称空间(Unwanted namespaces on SVG markup when using XMLSerializer in JavaScript with IE)

好吧,我想我已经解决了它。 从这篇文章跟踪这个WebKit错误报告和这个测试用例 。 如果我将脚本更改为此,则它可以工作: var el = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); el.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.w3.org/2000/svg'); el.setAttributeNS('http://...

XmlSerializer:删除不必要的xsi和xsd命名空间(XmlSerializer: remove unnecessary xsi and xsd namespaces)

由于Dave要求我在.NET中序列化对象时重复我省略所有xsi和xsd命名空间的答案,所以我更新了这篇文章,并从上述链接重复了我的回答。 在这个答案中使用的例子是与另一个问题相同的例子。 以下是逐字复制的。 在阅读Microsoft的文档和在线的几种解决方案之后,我发现了解决这个问题的方法。 它兼容内置的XmlSerializer和通过IXmlSerialiazble自定义XML序列化。 要使用whit,我将使用与此问题的答案中使用的相同的MyTypeWithNamespaces XML示例。 [...

如何让XMLSerializer将命名空间添加到嵌套对象中的属性?(How do I get the XMLSerializer to add namespaces to attributes in nested objects?)

IXmlSerializable也许? 注意我还添加了(对A ): [XmlElement("A", Namespace = "http://www.example.com/namespace")] public TestSoapHeaderTypeValuePair A {...} 代码如下: public class TestSoapHeaderTypeValuePair : IXmlSerializable { private string _type; private s...

使用XmlSerializer的多个名称空间(Multiple namespaces with XmlSerializer)

从XML的角度来看,你的例子是相同的,所以第一个例子很好。 如果您必须使用第二个,那么我们对XML的理解或处理流程存在严重问题。 From an XML point of view your examples are identical, so the first one is perfectly fine. If you have to use the second one, there is a serious problem with our XML understanding or you...

XmlSerializer在我的Windows 8商店应用程序中不返回任何内容(XmlSerializer returns nothing in my Windows 8 Store App)

这是你的问题, <XmlRoot("DATA", [Namespace]:="", IsNullable:=False)> 。 IsNullable属性设置为false时,如果项目等于空 ,则将省略项目的XML。 如果将IsNullable设置为True,那么它将发出类似<ListRoot xsi:nil = "true" />的标记。 在您的代码示例中,由于您刚刚创建了一个新的Data类,如Dim target As New Data() ,因此默认情况下所有成员都是Nothing 。 由于您已...

结合XmlSerializer和XmlWriter?(Combining XmlSerializer and XmlWriter?)

不,不要那样做。 如果需要序列化超过ObservableCollection,则定义包含类型并序列化它 。 public static void SaveBehaviors(ObservableCollection<Param> listParams) { XmlSerializer _paramsSerializer = new XmlSerializer(typeof(ContainingType)); var c = new ContainingType(listPar...

如何使用XmlSerializer反序列化带名称空间的xml文档?(How do I Deserialize xml document with namespaces using XmlSerializer?)

最初发布的评论,因为我没有验证它,但: <Prop1>Some Value</Prop1> 是不一样的 <ns0:Prop1>Some Value</ns0:Prop1> 所以为了让它工作,你可能只需要: [XmlElement(Namespace="")] public string Prop1 { get; set; } [XmlElement(Namespace="")] public string Prop2 { get; set; } Originally posted as a...

如何从多个名称空间中获取XmlSerializer反序列化属性?(How can I get XmlSerializer deserialize attributes from multiple namespaces? [closed])

我们提供的示例数据在命名空间中有一个typeo,导致它反序列化错误。 去搞清楚。 The sample data we were provided with had a typeo in the namespace which was causing it to deserialize wrong. Go figure.

自定义XmlSerializer仅在WebApi中添加名称空间(Custom XmlSerializer only adds namespaces in WebApi)

确保添加支持的媒体类型: public class SectionMediaTypeFormatter : BufferedMediaTypeFormatter { public SectionMediaTypeFormatter() { this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml")); this.SupportedMediaTypes.Add(new...

相关文章

更多

Guava Objects类详解

Objects.toStringHelper(s1)

如何实现:一个Tab中的List点击某个Item后想把该项添加到另一个tab中的ListView中?

一个Tab中的List点击某个Item后如何才能把该项添加到另一个tab中的ListView中? p ...

如何 通过TabPanel的add方法,调用已经存在的panel?

在菜单中 增加了 click点击事件,事件代码如下: 'click' : function(n){ ...

solr搜索提示,将词添加到词库中

solr wiki: http://wiki.apache.org/solr/Suggester/ 实 ...

Hadoop的I/O

1. 数据完整性:任何语言对IO的操作都要保持其数据的完整性。Hadoop当然希望数据在存储和处理中不 ...

Hadoop I/O系统介绍

看过很多Hadoop介绍或者是学习的帖子和文章,发现介绍Hadoop I/O系统的很少。很多文章都会介 ...

I18N 国际化 简介

软件的国际化: 软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提 ...

最新问答

更多

绝地求生、荒野行动、香肠派对 哪个更好玩???(都是吃鸡类游戏)

PC上的绝地求生,是最早也是最火的大逃杀游戏。 荒野行动是网易抄袭蓝洞绝地求生制作的手游。相似度90%,还有他一起出的终结折2,这2款正在被蓝洞告,打官司呢。 手游上的绝地求生有2部都是蓝洞授权(收钱)给腾讯开发的正版ID手游。所以跟PC上做的一模一样,蓝洞也没话说。 加上吃鸡国服也是腾讯独家代理,所以根本没有什么可说的。只要这个类型的 过于相似的,腾讯都可以借蓝洞之手起诉。打压同行是国内BAT最爱干的事嘛! 香肠派对画风虽然不一样,但核心玩法还是跟人家正版的一样的,同样也是没有被授权的。 98

如何在jQuery集合中选择第n个jQuery对象?(How to select the nth jQuery object in a jQuery collection?)

你可以使用eq : var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */ You can use eq: var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */

ASP NET使用jQuery和AJAX上传图像(ASP NET upload image with jQuery and AJAX)

您可以自己手动设置FormData键和值。 Upload 创建FormData并设置新的键/值 $("#btnUpload").on("click", function(e) { e.preventDefault(); var file = $("#imguploader").get(0).file

SQL Server XML查询中包含名称空间的位置(SQL Server XML query with namespaces in the where exist)

您可能希望使用#temp.identXml.query而不是#temp.identXml.query 。 您可以在这里阅读更多相关信息SQL Server XML exists() 我相信你也可以像这样使用它 Select #temp.identXml.value('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:SourceIndex)[1]','varchar(100)') as Ident ,#temp.identXml.value(

宁夏银川永宁县望远镇哪里有修mp5的?

胜利街有家电维修,电脑城,银川商场多得很…

我想用更新的日期标记所有更新的行(I would like to mark all updated rows with the date that they have been updated)

您可以使用更新后触发的触发器来执行此操作。 给出如下表: create table your_table (id int primary key, val int, last_update datetime) 每当您更新表中的内容时,此触发器将设置last_update值。 CREATE TRIGGER trigger_name ON your_table AFTER UPDATE AS BEGIN UPDATE your_table SET your_ta

郑州会计培训班

招生的,至于时间吗,就看你自己的时间段了,你可以致电0371-63300220.他们会帮你选择一下的。离你最近,最专业的培训班。

如何定位数组中的负数,并得到所有正数的总和?(How to target e negative number from an array, and get the sum of all positive numbers?)

只需创建一个条件来检查它是正数还是负数,然后定义一个空的数组negatives ,如果数字是负数,则将其推到负数组中,如果是正数,则将其添加到sum变量中,请查看下面的工作示例。 function SummPositive( numbers ) { var negatives = []; var sum = 0; for(var i = 0; i < numbers.length; i++) { if(numbers[i] < 0) { negati

在响应图像上叠加网格(Overlay grid on responsive image)

使用两个linear-gradient s,我们可以创建两个简单的线条,然后每隔n%重复一次background-size 。 它看起来像这样: background: linear-gradient(to bottom, #000 2px, transparent 2px), linear-gradient(to right, #000 2px, transparent 2px); background-size: 10%; 两个渐变创建两条相交的线,长度为百分比,如下所示: 使用默认的b

无法让POST在Azure网站上运行(Could not get POST to work on Azure Website)

最后我找到了答案......我不得不删除尾随的斜线! 我使用了“ https://example.com/api/messages/ ”,这将自动产生GET,无论我使用PostAsync还是PostAsJsonAsync。 使用“ https://example.com/api/messages”,GET和POST似乎都运行良好! Finally I've found the answer.... I had to remove the trailing slash! I've used "ht