如何读取复杂的XML
本文关键字:复杂 XML 读取 何读取 | 更新日期: 2023-09-27 18:10:23
我有以下XML
<departments>
<dept operationalStatus="active" primaryRole="Admin" depChangeDate="20130420">
<startDate type="legal">20130401</startDate>
<endDate type="legal"></endDate>
<startDate type="operational">20130320</startDate>
<endDate type="operational"></endDate>
<DeptRoles>
<DeptRole name="Other dept" status="active">
<startDate type="legal">20130401</startDate>
<endDate type="legal"></endDate>
<startDate type="operational">20130320</startDate>
<endDate type="operational"/>
<isPrimary/>
</DeptRole>
</DeptRoles>
</dept>
</departments>
我有大约200K的记录上传到数据库从xml文件。我想在一个表中从XML文件中获取以下数据operationalStatus, primaryRole, depChangeDate, startDate类型为legal及其值,startDate类型为operational及其值我可以访问到startDate类型的合法元素,但不能访问startDate类型的操作。并将以下数据从DeptRole元素移到另一个表中。DeptRole名称,状态,startDate类型合法及其值startDate类型操作及其值。什么是最好的方法,我该如何做到这一点。主要是我面临访问以下值
的问题 <startDate type="legal">20130401</startDate>
<endDate type="legal"></endDate>
<startDate type="operational">20130320</startDate>
<endDate type="operational"></endDate>
这有帮助吗?
string data = "<your xml data>";
XElement elem = XElement.Parse(data);
var departments = elem.Descendants("dept").ToList();
foreach (var dept in departments)
{
var sLegal = dept.Elements("startDate")
.First(p => p.Attribute("type").Value == "legal").Value;
var eLegal = dept.Elements("endDate")
.First(p => p.Attribute("type").Value == "legal").Value;
var sOp = dept.Elements("startDate")
.First(p => p.Attribute("type").Value == "operational").Value;
var eOp = dept.Elements("endDate")
.First(p => p.Attribute("type").Value == "operational").Value;
var attr=dept.Attribute("operationalStatus");
var opStatus = attr == null ? "" : attr.Value;
attr = dept.Attribute("primaryRole");
var primaryRole = attr == null ? "" : attr.Value;
attr = dept.Attribute("depChangeDate");
var depChangeDate = attr == null ? "" : attr.Value;
//do something with the values
}
有几个可用的选项,但是您会喜欢这个:
http://blogs.msdn.com/b/cdndevs/archive/2013/09/27/web-essentials-for-visual-studio-open-data-made-simple.aspx它允许你"复制"xml然后"粘贴"一个c#类。序列化或反序列化将生成用于生成该类的xml。
可以通过以下方式实现
- 将XML转换为DataSet()对象,并应用LINQ获取所需的列(或)
- 在XMLDocument上应用XPATH
——SJ
试试这个:Dim文档= XDocument. load ("PurchaseOrder.xml")Xdocument是来自系统的一个类。Linq命名空间所以从现在开始有很多方法可以通过Linq访问它
会成功的…
var readAll = from a in xd.Descendants("departments")
select new
{
DeptOperationalStatus = a.Element("dept").Attribute("operationalStatus").Value,
DeptPrimaryRole = a.Element("dept").Attribute("primaryRole").Value,
DeptChangeDate = a.Element("dept").Attribute("depChangeDate").Value,
LegalStartDate = a.Element("dept").Elements("startDate").First(cc => cc.Attribute("type").Value == "legal").Value,
LegalEndDate = a.Element("dept").Elements("endDate").First(cc => cc.Attribute("type").Value == "legal").Value,
OperationalStartDate = a.Element("dept").Elements("startDate").First(cc => cc.Attribute("type").Value == "operational").Value,
OperationEndDate = a.Elements("dept").Elements("endDate").First(cc => cc.Attribute("type").Value == "operational").Value,
// Dept Roles
DeptRoleName = a.Element("dept").Element("DeptRoles").Element("DeptRole").Attribute("name").Value,
DeptRoleStatus = a.Element("dept").Element("DeptRoles").Element("DeptRole").Attribute("status").Value,
DeptLegalStartDate = a.Element("dept").Element("DeptRoles").Element("DeptRole").Elements("startDate").First(cc => cc.Attribute("type").Value == "legal").Value,
DeptLegalEndDate = a.Element("dept").Element("DeptRoles").Element("DeptRole").Elements("endDate").First(cc => cc.Attribute("type").Value == "legal").Value,
DeptOperationalStartDate = a.Element("dept").Element("DeptRoles").Element("DeptRole").Elements("startDate").First(cc => cc.Attribute("type").Value == "operational").Value,
DeptOperationalEndDate = a.Element("dept").Element("DeptRoles").Element("DeptRole").Elements("endDate").First(cc => cc.Attribute("type").Value == "operational").Value,
};