我怎样才能得到这个XML结构

发布于 2024-07-13 14:14:15 字数 995 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2024-07-20 14:14:15

如果您知道所需的结构,最简单的选择是从 xml 进行操作; 将 xml 写入文件(在我的例子中是 foo.xml),然后(在命令行):

xsd foo.xml
xsd foo.xsd /classes

然后查看 foo.cs 以了解如何完成; 事实证明,您只需使用 [System.Xml.Serialization.XmlTextAttribute()] 标记该值即可。

这是 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;
        }
    }
}

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):

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()].

Here's the xsd output:

//------------------------------------------------------------------------------
// <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;
        }
    }
}
£烟消云散 2024-07-20 14:14:15

稍微了解了一下 Marc 的内容,但我猜测 Visual Studio 2005 和 2008 之间存在差异。

我需要将以下内容添加到“Value”元素的声明中。

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

看起来确实有效!

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.

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

It looks like it works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文