[英]Dealing with root nodes inside root nodes in XML with C#
Sorry for the horrible title, I wasn't sure what the proper phrasing is. 对不起,标题太糟糕了,我不确定正确的措词是什么。
I've been using this code to parse some XML: 我一直在使用此代码来解析一些XML:
[Serializable()]
public class Report
{
[XmlElement("AttachedFiles")]
public AttachedFiles attachedFiles;
public Report()
{
attachedFiles = null;
}
}
[Serializable()]
[XmlRoot("nodes")]
public class ReportList
{
[XmlElement("node", typeof(Report))]
public Report[] reportList;
}
[Serializable()]
[XmlRoot("AttachedFiles")]
public class AttachedFiles
{
[XmlElement("AttachedFile")]
public List<string> attachedFiles;
}
The XML looks something like this: XML看起来像这样:
<nodes>
<node>
<AttachedFiles>
<AttachedFile>File1</AttachedFile>
<AttachedFile>File2</AttachedFile>
</AttachedFiles>
</node>
</nodes>
The problem I'm having is that this ends up with me having to call Report.attachedFiles.attachedFiles
to get the List. 我遇到的问题是,这最终导致我不得不调用
Report.attachedFiles.attachedFiles
来获取列表。 Is there a way for me to only call Report.attachedFiles
and get the List? 有没有办法让我只调用
Report.attachedFiles
并获取列表? I know this is a really minor problem, but it's bothering quite a bit. 我知道这是一个非常小的问题,但是麻烦很多。
EDIT 编辑
This is what the code looks like after help from @xDaevax. 这是@xDaevax帮助后的代码。
[Serializable()]
public class Report
{
[XmlArray(ElementName = "AttachedFiles"), XmlArrayItem(ElementName = "AttachedFile")]
public List<string> AttachedFiles;
public Report()
{
attachedFiles = null;
}
}
[Serializable()]
[XmlRoot("nodes")]
public class ReportList
{
[XmlElement("node", typeof(Report))]
public Report[] reportList;
}
Thanks for the help! 谢谢您的帮助!
Here is what I came up with (though I did not have your JiraReport class handy). 这是我想出的(尽管我没有方便的JiraReport类)。
[Serializable()]
public class Report {
[XmlIgnore()]
private AttachedFiles _attachedFiles;
public Report() {
attachedFiles = null;
} // end constructor
[XmlArray(ElementName = "AttachedFiles"), XmlArrayItem(ElementName = "AttachedFile")]
public AttachedFiles Files {
get { return _attachedFiles; }
set { _attachedFiles = value; }
} // end property Files
} // end class Report
[Serializable()]
[XmlRoot("ReportList")]
public class ReportList {
[XmlIgnore()]
private Report[] _reports;
public ReportList() {
_reports = null;
} // end constructor
[XmlArray(ElementName = "nodes"), XmlArrayItem(ElementName = "node")]
public Report[] Reports {
get { return _reports; }
set { _reports = value; }
} // end property Reports
} // end class ReportList
[Serializable()]
public class AttachedFiles : List<string> {
} // end class AttachedFiles
Using the XmlArray and ArrayItem on a collection property simplifies the attribute usage and makes the code easier to read. 在集合属性上使用XmlArray和ArrayItem可以简化属性用法,并使代码更易于阅读。 Also, because the AttachedFiles class inherits from a list of string, it removes a level of depth from your object graph so the call isn't redundant anymore.
另外,由于AttachedFiles类继承自字符串列表,因此它从对象图中删除了深度级别,因此该调用不再多余。 You could also add a function on the ReportList class that loops and returns all attached files for all reports for you (functions will not be serialized by the XmlSerializer).
您还可以在ReportList类上添加一个函数,该函数循环并为您返回所有报告的所有附件(XmlSerializer不会序列化这些函数)。
Edit: Here is some MSDN documentation that explains the feature usage: 编辑:这是一些MSDN文档,说明功能用法:
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayitemattribute(v=vs.100).aspx Also, see this BlackWasp article that has a nice walkthrough of XML serialization involving arrays. http://msdn.microsoft.com/zh-cn/library/system.xml.serialization.xmlarrayitemattribute(v=vs.100).aspx另外,请参阅这篇BlackWasp文章,它很好地介绍了涉及数组的XML序列化。 http://www.blackwasp.co.uk/xmlarrays.aspx
http://www.blackwasp.co.uk/xmlarrays.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.