我怎样才能得到这个XML结构
我有一个 Web 服务,其输入对象类似于以下内容。
public class MyInput
{
[System.Xml.Serialization.XmlArrayItem("Demographic")]
public DemographicsInfo[] Demographics {get; set;}
}
DemographicsInfo 类的定义如下。
public class DemographicsInfo
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name { get; set; }
public string Value { get; set; }
}
现在这会生成一个像这样的 XML 结构。
<Demographics>
<Demographic Name="String">
<value>string</value>
</Demographic>
<Demographic Name="String">
<value>string</value>
</Demographic>
</Demographics>
我需要将其纳入此
<Demographics>
<Demographic Name="String">string</Demographic>
<Demographic Name="String">string</Demographic>
</Demographics>
对于我的一生,我似乎无法找到适用于获得此格式的正确属性。 有人有建议吗?
I have a web service that has an input object similar to the following.
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.
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您知道所需的结构,最简单的选择是从 xml 进行操作; 将 xml 写入文件(在我的例子中是
foo.xml
),然后(在命令行):然后查看
foo.cs
以了解如何完成; 事实证明,您只需使用[System.Xml.Serialization.XmlTextAttribute()] 标记该值即可。
这是 xsd 输出:
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):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()]
.Here's the xsd output:
稍微了解了一下 Marc 的内容,但我猜测 Visual Studio 2005 和 2008 之间存在差异。
我需要将以下内容添加到“Value”元素的声明中。
看起来确实有效!
Looking a bit on what Marc has, but I'm guessing a difference between Visual Studio 2005 and 2008.
I needed to add the following to the declaration of the "Value" element.
It looks like it works!