如何装饰/定义要与 XmlSerializer 一起使用的可选 XML 元素的类成员?
我有以下 XML 结构。 theElement
元素可以包含 theOptionalList
元素,也可以不包含:
<theElement attrOne="valueOne" attrTwo="valueTwo">
<theOptionalList>
<theListItem attrA="valueA" />
<theListItem attrA="anotherValue" />
<theListItem attrA="stillAnother" />
</theOptionalList>
</theElement>
<theElement attrOne="anotherOne" attrTwo="anotherTwo" />
什么是表达相应类结构的简洁方法?
我非常确定以下几点:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace MyNamespace
{
public class TheOptionalList
{
[XmlAttributeAttribute("attrOne")]
public string AttrOne { get; set; }
[XmlAttributeAttribute("attrTwo")]
public string AttrTwo { get; set; }
[XmlArrayItem("theListItem", typeof(TheListItem))]
public TheListItem[] theListItems{ get; set; }
public override string ToString()
{
StringBuilder outText = new StringBuilder();
outText.Append("attrOne = " + AttrOne + " attrTwo = " + AttrTwo + "\r\n");
foreach (TheListItem li in theListItems)
{
outText.Append(li.ToString());
}
return outText.ToString();
}
}
}
以及:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace MyNamespace
{
public class TheListItem
{
[XmlAttributeAttribute("attrA")]
public string AttrA { get; set; }
public override string ToString()
{
StringBuilder outText = new StringBuilder();
outText.Append(" attrA = " + AttrA + "\r\n");
return outText.ToString();
}
}
}
但是 theElement
又如何呢?我是否将 theOptionalList
元素作为数组类型,让它读取在文件中找到的内容(什么都没有,或者一个),然后检查代码是否存在?或者我可以提供其他装饰器吗?或者它只是有效吗?
编辑:我最终使用了来自这个答案。
I have the following XML structure. The theElement
element can contain theOptionalList
element, or not:
<theElement attrOne="valueOne" attrTwo="valueTwo">
<theOptionalList>
<theListItem attrA="valueA" />
<theListItem attrA="anotherValue" />
<theListItem attrA="stillAnother" />
</theOptionalList>
</theElement>
<theElement attrOne="anotherOne" attrTwo="anotherTwo" />
What is a clean way to express the corresponding class structure?
I'm pretty sure of the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace MyNamespace
{
public class TheOptionalList
{
[XmlAttributeAttribute("attrOne")]
public string AttrOne { get; set; }
[XmlAttributeAttribute("attrTwo")]
public string AttrTwo { get; set; }
[XmlArrayItem("theListItem", typeof(TheListItem))]
public TheListItem[] theListItems{ get; set; }
public override string ToString()
{
StringBuilder outText = new StringBuilder();
outText.Append("attrOne = " + AttrOne + " attrTwo = " + AttrTwo + "\r\n");
foreach (TheListItem li in theListItems)
{
outText.Append(li.ToString());
}
return outText.ToString();
}
}
}
As well as:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace MyNamespace
{
public class TheListItem
{
[XmlAttributeAttribute("attrA")]
public string AttrA { get; set; }
public override string ToString()
{
StringBuilder outText = new StringBuilder();
outText.Append(" attrA = " + AttrA + "\r\n");
return outText.ToString();
}
}
}
But what about for theElement
? Do I take the theOptionalList
element as an array type to have it read what it finds in the file (either nothing, or one) and then check in code whether it's there or not? Or is there another decorator that I can supply? Or does it just work?
EDIT: I ended up using information from this answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将
IsNullable = true
添加到XmlArrayItem
属性。Try adding
IsNullable = true
to theXmlArrayItem
attribute.看起来您可以使用另一个布尔值来指定是否包含元素。
http://msdn.microsoft.com/en-us /library/system.xml.serialization.xmlserializer.aspx
It looks like you can use another bool to specify to include an element or not.
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
除了
XxySpecifed
属性之外,还有一个带有ShouldSerialize
前缀的方法Additional to the
XxySpecifed
property, there is also a method with aShouldSerialize
prefix