表示不带 xsd 的 XML
注意:我无法使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 XML 上运行 xsd.exe 两次以创建中间 XSD,然后从中创建 C# 类会产生以下结果:
这对您有用吗?
Running
xsd.exe
twice on your XML to create an intermediary XSD and then a C# class from it yields this result:Does that work for you??
您尝试生成的 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.
我认为您需要使用属性
[ XmlText]
:I think you need to use the attribute
[XmlText]
: