我有一个复杂的XML结构,分层的Process,Stage,Step,Task如下:
<Process>
<Stage StageName="Data gathering" StagePage="page_DA.html" Status="Not Started" Selected="true">
<StageUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</StageUsers>
<Step StepName="Data acquistion - General" StepPage="page_DAGeneral.html" Status="Not Started">
<StepUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</StepUsers>
<Task TaskName="Well locations and reference elevations" TaskTip="Well locations and reference elevations" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Well deviation surveys" TaskTip="Well deviation surveys" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
</Step>
<Step StepName="Data acquistion - Petrophysics" StepPage="page_DAPetro.html" Status="Not Started">
<StepUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</StepUsers>
<Task TaskName="Digitization of maps and logs" TaskTip="Digitization of maps and logs" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Log data loading" TaskTip="Log data loading" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Special core analysis" TaskTip="Special core analysis" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
</Step>
<Step StepName="Data acquistion - Geology" StepPage="page_DAGeology.html" Status="Not Started">
<StepUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</StepUsers>
<Task TaskName="Stratigraphic correlations" TaskTip="Stratigraphic correlations" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Core data" TaskTip="Core data" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Geochemistry data" TaskTip="Geochemistry data" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Dip interpretation data" TaskTip="Dip interpretation data" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Existing digital time or depth structure maps" TaskTip="Existing digital time or depth structure maps" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Existing digital property maps" TaskTip="Existing digital property maps" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
</Step>
<Step StepName="Data acquistion - Geophysics" StepPage="page_DAGeophysics.html" Status="Not Started">
<StepUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</StepUsers>
<Task TaskName="Seismic data loading" TaskTip="Seismic data loading" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Stacking velocity ata loading" TaskTip="Stacking velocity ata loading" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="VSP or check shot data" TaskTip="VSP or check shot data" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
</Step>
<Step StepName="Data acquistion - Engineering" StepPage="page_DAEng.html" Status="Not Started">
<StepUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</StepUsers>
<Task TaskName="Drilling and completion data" TaskTip="Drilling and completion data" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Pressure transient data" TaskTip="Pressure transient data" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Production data" TaskTip="Production data" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Pressure data" TaskTip="Pressure data" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Sample chracterization" TaskTip="Sample chracterization" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
</Step>
<Step StepName="Data acquistion - Additional" StepPage="page_DAAdd.html" Status="Not Started">
<StepUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</StepUsers>
<Task TaskName="New cores" TaskTip="New cores" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Special core analysis for Geomechanics" TaskTip="Special core analysis for Geomechanics" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Core sampling" TaskTip="Core sampling" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
<Task TaskName="Laboratory tests" TaskTip="Laboratory tests" Status="Not Started">
<TaskUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</TaskUsers>
</Task>
</Step>
</Stage>
<Stage StageName="Well reservoir production review" StagePage="page_WRPR.html" Status="Not Started" Selected="true">
<StageUsers>
<User Alias="PNeelakandan" FullName="Prasaanth Neelakandan" Role="Dev" Email="PNeelakandan@exchange.com" Organization="Windows"/>
<User Alias="SGuo3" FullName="Wen Jiao" Role="Dev" Email="SGuo3@exchange.com" Organization="Windows"/>
</StageUsers>
</Stage>
</Process>
基本上,流程由阶段组成。阶段由步骤组成,步骤由任务组成。其中每个都有一些已分配的用户和一些属性,如名称,页面,状态.etc。
所有这些都有自己的类:
public class Process
{
public string ProcessName { get; set; }
public string HelpFile { get; set; }
public List<Stage> Stages { get; set; }
public Process()
{
Stages = new List<Stage>();
}
public XElement GenerateXML()
{
var proc = new XElement("Process");
foreach (Stage stg in Stages)
{
proc.Add(stg.GenerateXML());
}
// this returns the final Process XML
return proc;
}
}
舞台类:
public class Stage
{
public string StageName { get; set; }
public string stagepage { get; set; }
public List<Step> Steps { get; set; }
public List<User> StageUsers { get; set; }
public string StageStatus { get; set; }
public bool IsSelected;
public Stage(String name)
{
StageName = name;
Steps = new List<Step>();
StageUsers = new List<User>();
Selected = true;
}
public XElement GenerateXML()
{
var proj = new XElement("Stage",
new XAttribute("StageName", StageName),
new XAttribute("StagePage", stagepage),
new XAttribute("Status", StageStatus),
new XAttribute("Selected", IsSelected)
);
var users = new XElement("StageUsers");
foreach (User stgUser in StageUsers)
{
users.Add(stgUser.GenerateXML());
}
proj.Add(users);
foreach (Step st in Steps)
{
proj.Add(st.GenerateXML());
}
return proj;
}
}
步骤类:
public class Step
{
public string StepName { get; set; }
public string StepPage { get; set; }
public List<Task> Tasks { get; set; }
public List<User> StepUsers { get; set; }
public string StepStatus { get; set; }
public Step(String name)
{
StepName = name;
Tasks = new List<Task>();
StepUsers=new List<User>();
}
public XElement GenerateXML()
{
var proj = new XElement("Step",
new XAttribute("StepName", StepName),
new XAttribute("StepPage", StepPage),
new XAttribute("Status", StepStatus)
);
var users = new XElement("StepUsers");
foreach (User stpUser in StepUsers)
{
users.Add(stpUser.GenerateXML());
}
proj.Add(users);
foreach (Task tk in Tasks)
{
proj.Add(tk.GenerateXML());
}
return proj;
}
}
任务类:
public class Task
{
public string TaskTip { get; set; }
public string TaskName { get; set; }
public List<User> TaskUsers { get; set; }
public string TaskStatus { get; set; }
public Task(String name)
{
TaskName = name;
TaskUsers = new List<User>();
}
public XElement GenerateXML()
{
var proj = new XElement("Task",
new XAttribute("TaskName", TaskName),
new XAttribute("TaskTip", TaskTip)
,
new XAttribute("Status", TaskStatus)
);
var users = new XElement("TaskUsers");
foreach (User tkUser in TaskUsers)
{
users.Add(tkUser.GenerateXML());
}
proj.Add(users);
return proj;
}
}
用户类:
public class User
{
public string Alias { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string Role { get; set; }
public string Organization { get; set; }
public XElement GenerateXML()
{
var proj = new XElement("User",
new XAttribute("Alias", Alias),
new XAttribute("FullName", FullName),
new XAttribute("Role", Role),
new XAttribute("Email", Email),
new XAttribute("Organization", Organization)
);
return proj;
}
}
我已经为eahc步骤编写了GenerateXML()方法,以获取上面显示的最终XML(从EFDP用户调用时)。
我的问题是:有没有办法可以将上述XML读入各自的类和对象中,如图所示?我尝试过XML Serialization但是无法弄清楚如何为属性做这个? 我总是可以尝试以蛮力的方式将其读入对象,但这将是非常冗长的。
如果我能对此有任何想法,或者我可以获得任何改进上述设计的建议,以便能够轻松地将XML读入对象,那将是非常好的。
public class Reviewer
{
[XmlAttribute]
public string Verdict { get; set; }
[XmlAttribute]
public string Comments { get; set; }
[XmlElement]
public User User { get; set; }
public Reviewer()
{
User = new User();
}
}
答案 0 :(得分:4)
通过将XmlSerializer
:属性应用于属性,您可以使用[XmlAttribute]
将类的属性序列化为XML属性:
public class User
{
[XmlAttribute]
public string Alias { get; set; }
[XmlAttribute]
public string FullName { get; set; }
[XmlAttribute]
public string Email { get; set; }
[XmlAttribute]
public string Role { get; set; }
[XmlAttribute]
public string Organization { get; set; }
}
有关控制和自定义.Net类的XML序列化的更多信息,请参阅此处:Attributes That Control XML Serialization。
完成此操作后,您现在可以使用以下扩展方法将类序列化为XElement
或XML字符串:
public static class XmlSerializationExtensions
{
public static string ToXml<T>(this T obj)
{
return obj.ToXml(false);
}
public static string ToXml<T>(this T obj, bool omitStandardNamespaces)
{
using (var textWriter = new StringWriter())
{
var settings = new XmlWriterSettings() { Indent = true, IndentChars = " " }; // For cosmetic purposes.
using (var writer = XmlWriter.Create(textWriter, settings))
{
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
(ns = new XmlSerializerNamespaces()).Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
new XmlSerializer(obj.GetType()).Serialize(writer, obj, ns);
}
return textWriter.ToString();
}
}
public static T DeserializeXML<T>(this string xmlString)
{
using (var reader = new StringReader(xmlString))
{
var result = new XmlSerializer(typeof(T)).Deserialize(reader);
if (result is T)
return (T)result;
}
return default(T);
}
public static XElement ToXElement<T>(this T obj)
{
return obj.ToXElement(true);
}
public static XElement ToXElement<T>(this T obj, bool omitStandardNamespaces)
{
var doc = new XDocument();
using (var writer = doc.CreateWriter())
{
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
(ns = new XmlSerializerNamespaces()).Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
new XmlSerializer(obj.GetType()).Serialize(writer, obj, ns);
}
var element = doc.Root;
if (element != null)
element.Remove();
return element;
}
public static T DeserializeXML<T>(this XContainer element)
{
using (var reader = element.CreateReader())
{
var result = new XmlSerializer(typeof(T)).Deserialize(reader);
if (result is T)
return (T)result;
}
return default(T);
}
}
然后你可以像这样使用扩展方法:
var user = new User { Alias = "foo", Email = "bar@none.net", FullName = "full name name", Organization = "disorganized", Role = "forward" };
var xml = user.ToXml();
Debug.WriteLine(xml);
var xElement = user.ToXElement();
Debug.WriteLine(xElement);
或者,反序列化:
var process1 = xmlString.DeserializeXML<Process>();
var process2 = xmlElement.DeserializeXML<Process>();
<强>更新强>
要使用XmlSerializer
,您的所有课程必须具有公开(可能是默认)parameterless constructor。此外,在您的XML中,您的某些列表深度为一级,而有些列表深度为两级。一级深度列表需要使用XmlElement
属性进行修饰,如下所示:
[XmlElement("Stage")]
public List<Stage> Stages { get; set; }
您还有一些名称不一致。可以通过指定element name或attribute name来修复这些问题,例如:
[XmlAttribute("Selected")]
public bool IsSelected { get; set; }
因此,以下一组类似乎在您的示例XML中成功读取:
public class Process
{
public string ProcessName { get; set; }
public string HelpFile { get; set; }
[XmlElement("Stage")]
public List<Stage> Stages { get; set; }
public Process()
{
Stages = new List<Stage>();
}
}
public class Stage
{
[XmlAttribute]
public string StageName { get; set; }
[XmlAttribute("StagePage")]
public string stagepage { get; set; }
[XmlAttribute("Status")]
public string StageStatus { get; set; }
[XmlAttribute("Selected")]
public bool IsSelected { get; set; }
[XmlElement("Step")]
public List<Step> Steps { get; set; }
public List<User> StageUsers { get; set; }
public Stage() : this(null) { }
public Stage(String name)
{
StageName = name;
Steps = new List<Step>();
StageUsers = new List<User>();
IsSelected = true;
}
}
public class Step
{
[XmlAttribute]
public string StepName { get; set; }
[XmlAttribute]
public string StepPage { get; set; }
[XmlAttribute("Status")]
public string StepStatus { get; set; }
[XmlElement("Task")]
public List<Task> Tasks { get; set; }
public List<User> StepUsers { get; set; }
public Step() : this(null) { }
public Step(String name)
{
StepName = name;
Tasks = new List<Task>();
StepUsers = new List<User>();
}
}
public class Task
{
[XmlAttribute]
public string TaskName { get; set; }
[XmlAttribute]
public string TaskTip { get; set; }
[XmlAttribute("Status")]
public string TaskStatus { get; set; }
public List<User> TaskUsers { get; set; }
public Task() : this(null) { }
public Task(String name)
{
TaskName = name;
TaskUsers = new List<User>();
}
}
public class User
{
[XmlAttribute]
public string Alias { get; set; }
[XmlAttribute]
public string FullName { get; set; }
[XmlAttribute]
public string Email { get; set; }
[XmlAttribute]
public string Role { get; set; }
[XmlAttribute]
public string Organization { get; set; }
}