【问题标题】:how to read complex XML如何阅读复杂的 XML
【发布时间】:2014-06-25 10:55:12
【问题描述】:

我有以下 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 类型合法及其值和 startDate 类型可操作及其值 我可以访问 startDate 类型的合法元素,但无法访问 startDate 类型的操作。 并将 DeptRole 元素中的以下数据放入另一个表中。 DeptRole 名称、状态、startDate 类型合法及其值和 startDate 类型可操作及其值。 什么是最好的方法,我该怎么做。 主要是我在访问以下值时遇到问题

    <startDate type="legal">20130401</startDate>
<endDate type="legal"></endDate>
<startDate type="operational">20130320</startDate>
<endDate type="operational"></endDate>  

【问题讨论】:

  • 您是如何尝试访问这些值的?
  • @DanielKelley by if (el.Element("startDate").Attribute("type").ToString() == "legal"
  • @DanielKelley if (el.Element("startDate").Attribute("type").ToString() == "operational"
  • @DanielKelley 法律价值是成功的,但如果声明永远不正确则可操作

标签: c# sql-server xml linq-to-xml


【解决方案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
}

【讨论】:

  • 非常感谢您的帮助,但我如何添加其他属性 operationStatus、primaryRole、depChangeDate
  • 非常感谢您的帮助。如果元素或属性存在于数据中,我如何验证它们。例如 我如何检查它是否存在。因为这不是所有记录中都存在的。和属性,例如 dept 元素中的操作状态。请帮我解决这个问题....
  • 使用 dept.Element("") 或 dept.Attribute("")。如果它们为空,则它们会丢失。最后几行代码为属性执行此操作
【解决方案2】:

有多种选择,但您会喜欢这个:

http://blogs.msdn.com/b/cdndevs/archive/2013/09/27/web-essentials-for-visual-studio-open-data-made-simple.aspx

它允许您“复制”xml,然后“粘贴”一个 c# 类。序列化或反序列化将产生用于生成该类的 xml。

【讨论】:

    【解决方案3】:

    可以通过以下方式实现

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

    --SJ

    【讨论】:

    【解决方案4】:

    试试这个: Dim doc As XDocument = XDocument.Load("PurchaseOrder.xml") Xdocument 是 System.Linq 命名空间中的一个类,因此从这一点开始,可以通过 LINQ 访问它。

    【讨论】:

      【解决方案5】:

      它会起作用的......

        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,
      
      
      
                            };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多