我有以下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类型操作及其值 我可以访问startDate类型的法律元素,但无法访问startDate类型操作。 以及来自DeptRole元素的以下数据到另一个表中。 DeptRole名称,status,startDate类型legal及其值和startDate类型操作及其值。 什么是最好的方法,我该怎么做。 主要是我遇到访问以下值的问题
<startDate type="legal">20130401</startDate>
<endDate type="legal"></endDate>
<startDate type="operational">20130320</startDate>
<endDate type="operational"></endDate>
答案 0 :(得分:1)
这有帮助吗?
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
}
答案 1 :(得分:1)
有几种选择,但您会喜欢这个:
它允许您复制&#39; xml然后&#39;粘贴&#39;一个c#类。序列化或反序列化将导致您用于生成该类的xml。
答案 2 :(得分:0)
可以通过以下方式实现
- SJ
答案 3 :(得分:0)
试试这个: Dim doc As XDocument = XDocument.Load(&#34; PurchaseOrder.xml&#34;) Xdocument是System.Linq命名空间中的一个类,所以从这一点开始有一些方法可以通过LINQ访问它。
答案 4 :(得分:0)
它会起作用......
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,
};