I have a web service that has an input object similar to the following.
我有一个web服务,它有一个类似于下面的输入对象。
public class MyInput
{
[System.Xml.Serialization.XmlArrayItem("Demographic")]
public DemographicsInfo[] Demographics {get; set;}
}
With the definition of the DemographicsInfo class like this.
根据人口统计学的定义。
public class DemographicsInfo
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name { get; set; }
public string Value { get; set; }
}
Right now this generates an XML structure like this.
现在,它生成这样的XML结构。
<Demographics>
<Demographic Name="String">
<value>string</value>
</Demographic>
<Demographic Name="String">
<value>string</value>
</Demographic>
</Demographics>
I need to get it into this
我需要把它写进去
<Demographics>
<Demographic Name="String">string</Demographic>
<Demographic Name="String">string</Demographic>
</Demographics>
For the life of me, I cannot seem to find the proper attribute(s) to apply to get this format. Does anyone have any advice?
对于我的生活,我似乎找不到合适的属性来得到这种格式。有人有什么建议吗?
2 个解决方案
#1
5
If you know the structure you want, the simplest option is to work back from the xml; write the xml into a file (foo.xml
in my case), then (at the command line):
如果您知道您想要的结构,那么最简单的选择就是从xml返回;将xml写入文件(foo)。然后(在命令行):
xsd foo.xml
xsd foo.xsd /classes
Then look at foo.cs
to see how it can be done; it turns out that you simply mark the value with [System.Xml.Serialization.XmlTextAttribute()]
.
然后看看foo。看看如何做到这一点;结果表明,您只需使用[system . xml . serializ.xmltextattribute()]标记该值。
Here's the xsd output:
xsd的输出:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3053
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Demographics {
private DemographicsDemographic[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Demographic", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public DemographicsDemographic[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class DemographicsDemographic {
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
#2
1
Looking a bit on what Marc has, but I'm guessing a difference between Visual Studio 2005 and 2008.
看看Marc的作品,我猜是Visual Studio 2005和2008的不同之处。
I needed to add the following to the declaration of the "Value" element.
我需要将以下内容添加到“Value”元素的声明中。
[System.Xml.Serialization.XmlText()]
public string Value { get; set; }
It looks like it works!
看起来很管用!
#1
5
If you know the structure you want, the simplest option is to work back from the xml; write the xml into a file (foo.xml
in my case), then (at the command line):
如果您知道您想要的结构,那么最简单的选择就是从xml返回;将xml写入文件(foo)。然后(在命令行):
xsd foo.xml
xsd foo.xsd /classes
Then look at foo.cs
to see how it can be done; it turns out that you simply mark the value with [System.Xml.Serialization.XmlTextAttribute()]
.
然后看看foo。看看如何做到这一点;结果表明,您只需使用[system . xml . serializ.xmltextattribute()]标记该值。
Here's the xsd output:
xsd的输出:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3053
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Demographics {
private DemographicsDemographic[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Demographic", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public DemographicsDemographic[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class DemographicsDemographic {
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
#2
1
Looking a bit on what Marc has, but I'm guessing a difference between Visual Studio 2005 and 2008.
看看Marc的作品,我猜是Visual Studio 2005和2008的不同之处。
I needed to add the following to the declaration of the "Value" element.
我需要将以下内容添加到“Value”元素的声明中。
[System.Xml.Serialization.XmlText()]
public string Value { get; set; }
It looks like it works!
看起来很管用!