繁体   English   中英

如何读取复杂的XML

[英]how to read complex XML

我有以下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文件中的一个表中获取以下数据的数据operationStatus,primaryRole,depChangeDate,startDate类型Legal及其值和startDate类型Operational及其值我可以访问startDate类型Legal元素,但不能访问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,然后“粘贴” ac#类。 序列化或反序列化将产生用于生成该类的xml。

可以通过以下方式实现

  1. 将XML转换为DataSet()对象,并应用LINQ来获取所需的列(或)
  2. 在XMLDocument上套用XPATH

--SJ

尝试以下操作:Dim doc As XDocument = XDocument.Load(“ PurchaseOrder.xml”)Xdocument是System.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,



                      };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM