表示不带 xsd 的 XML

发布于 2024-08-19 23:47:47 字数 682 浏览 4 评论 0原文

注意:我无法使用 XSD...不会详细说明原因。

我在正确表示应反序列化为的类中的以下 xml 时遇到问题:

XML:

<product>
   <sku>oursku</sku>
   <attribute name="attrib1">value1</attribute>
   <attribute name="attrib2">value2</attribute>
   <attribute name="attribx">valuex</attribute>
</product>

问题是表示属性节点

到目前为止我所拥有的是:

[XmlElement(ElementName = "Attribute")]
public Attribute[] productAttributes;

public class Attribute
{
    [XmlAttribute(AttributeName = "Name")]
    public string attributeName;

    public Attribute()
    {

    }
}

我知道我缺少一些东西来存储值,也许

Note: I cannot use XSD... not going to go into why.

I'm having a problem properly representing the following xml in a class that it should get deserialized into:

XML:

<product>
   <sku>oursku</sku>
   <attribute name="attrib1">value1</attribute>
   <attribute name="attrib2">value2</attribute>
   <attribute name="attribx">valuex</attribute>
</product>

the problem is representing the attribute nodes

What I have so far is:

[XmlElement(ElementName = "Attribute")]
public Attribute[] productAttributes;

public class Attribute
{
    [XmlAttribute(AttributeName = "Name")]
    public string attributeName;

    public Attribute()
    {

    }
}

I know I'm missing something to store the value, and perhaps

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

初见 2024-08-26 23:47:47

在 XML 上运行 xsd.exe 两次以创建中间 XSD,然后从中创建 C# 类会产生以下结果:

[Serializable]
[XmlType(AnonymousType=true)]
[XmlRoot(Namespace="", IsNullable=false)]
public partial class product 
{
    private string skuField;
    private productAttribute[] attributeField;

    [XmlElement(Form=XmlSchemaForm.Unqualified)]
    public string sku {
        get {
            return this.skuField;
        }
        set {
            this.skuField = value;
        }
    }

    [XmlElement("attribute", Form=XmlSchemaForm.Unqualified, IsNullable=true)]
    public productAttribute[] attribute {
        get {
            return this.attributeField;
        }
        set {
            this.attributeField = value;
        }
    }
}

[Serializable]
[XmlType(AnonymousType=true)]
public partial class productAttribute {

    private string nameField;
    private string valueField;

    [XmlAttribute]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    [XmlText]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

这对您有用吗?

Running xsd.exe twice on your XML to create an intermediary XSD and then a C# class from it yields this result:

[Serializable]
[XmlType(AnonymousType=true)]
[XmlRoot(Namespace="", IsNullable=false)]
public partial class product 
{
    private string skuField;
    private productAttribute[] attributeField;

    [XmlElement(Form=XmlSchemaForm.Unqualified)]
    public string sku {
        get {
            return this.skuField;
        }
        set {
            this.skuField = value;
        }
    }

    [XmlElement("attribute", Form=XmlSchemaForm.Unqualified, IsNullable=true)]
    public productAttribute[] attribute {
        get {
            return this.attributeField;
        }
        set {
            this.attributeField = value;
        }
    }
}

[Serializable]
[XmlType(AnonymousType=true)]
public partial class productAttribute {

    private string nameField;
    private string valueField;

    [XmlAttribute]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    [XmlText]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

Does that work for you??

拥醉 2024-08-26 23:47:47

您尝试生成的 XML 看起来与 XmlSerializer 能够本地创建的类型不同。我认为您将必须实现 IXmlSerialized 并自定义编写它。

The XML you're trying to produce doesn't look like the sort that XmlSerializer is capable of creating natively. I think you're going to have to implement IXmlSerializable and custom-write it.

不知所踪 2024-08-26 23:47:47

我认为您需要使用属性 [ XmlText]

public class Attribute
{
    [XmlAttribute(AttributeName = "Name")]
    public string attributeName;

    [XmlText]
    public string Value {get;set;}

    public Attribute()
    {

    }
}

I think you need to use the attribute [XmlText]:

public class Attribute
{
    [XmlAttribute(AttributeName = "Name")]
    public string attributeName;

    [XmlText]
    public string Value {get;set;}

    public Attribute()
    {

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