我正在try 将格式如下的XML文件加载到数据表:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<controls:controls xmlns="http://scap.nist.gov/schema/sp800-53/2.0"
                   xmlns:controls="http://scap.nist.gov/schema/sp800-53/feed/2.0"
                   xmlns:xhtml="http://www.w3.org/1999/xhtml"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   pub_date="2017-08-01"
                   xsi:schemaLocation="http://scap.nist.gov/schema/sp800-53/feed/2.0 http://scap.nist.gov/schema/sp800-53/feed/2.0/sp800-53-feed_2.0.xsd">
   <controls:control>
      <family>ACCESS CONTROL</family>
      <number>AC-1</number>
      <title>POLICY AND PROCEDURES</title>
      <baseline>LOW</baseline>
      <baseline>MODERATE</baseline>
      <baseline>HIGH</baseline>
      <baseline>PRIVACY</baseline>
      <statement>
         <description/>
         <statement>
            <number>AC-1a.</number>
            <description>Develop, document, and disseminate to [Assignment: organization-defined personnel or roles]:</description>
            <statement>
               <number>AC-1a.1.</number>
               <description>[Selection (one or more): Organization-level; Mission/business process-level; System-level] access control policy that:</description>
               <statement>
                  <number>AC-1a.1.(a)</number>
                  <description>Addresses purpose, scope, roles, responsibilities, management commitment, coordination among organizational entities, and compliance; and</description>
               </statement>
               <statement>
                  <number>AC-1a.1.(b)</number>
                  <description>Is consistent with applicable laws, executive orders, directives, regulations, policies, standards, and guidelines; and</description>
               </statement>
            </statement>
            <statement>
               <number>AC-1a.2.</number>
               <description>Procedures to facilitate the implementation of the access control policy and the associated access controls;</description>
            </statement>
         </statement>
         <statement>
            <number>AC-1b.</number>
            <description>Designate an [Assignment: organization-defined official] to manage the development, documentation, and dissemination of the access control policy and procedures; and</description>
         </statement>
         <statement>
            <number>AC-1c.</number>
            <description>Review and update the current access control:</description>
            <statement>
               <number>AC-1c.1.</number>
               <description>Policy [Assignment: organization-defined frequency] and following [Assignment: organization-defined events]; and</description>
            </statement>
            <statement>
               <number>AC-1c.2.</number>
               <description>Procedures [Assignment: organization-defined frequency] and following [Assignment: organization-defined events].</description>
            </statement>
         </statement>
      </statement>
</controls:control>

我能够提取家庭和其他值,但当我试图访问这些语句时,我什么也得不到.我需要创建一个表,其中每个语句值都在一行中,父值也在该行中,如Family和TITLE.

这就是我所得到的:

        string ns = "http://scap.nist.gov/schema/sp800-53/feed/2.0";

        XDocument xdoc = XDocument.Load("80053.xml");

        var ccidata = xdoc.Descendants(XName.Get("control", ns))
            .Descendants("statement")
            .Select(i => new
            {
                family = i?.Parent?.Element("family")?.Value,
                number = i?.Element("number")?.Value,
            }).ToList();

如果您能给我指点,我将不胜感激.

推荐答案

我更喜欢使用XMLLinq(XDocument).您缺少命名空间.请参阅下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication23
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            List<Ccidata> ccidata = Ccidata.GetData(FILENAME); 

        }
    }
    public class Ccidata
    {
        public string description { get; set; }
        public string number { get; set; }

        public static List<Ccidata> GetData(string filename)
        {
            XDocument doc = XDocument.Load(filename);
            XElement control = doc.Root;
            XNamespace ns = control.GetDefaultNamespace();
            XNamespace nsControl = control.GetNamespaceOfPrefix("controls");

            List<Ccidata> ccidata = doc.Descendants(ns + "statement")
                .Select(x => new Ccidata() { description = (string)x.Element(ns + "description"), number = (string)x.Element(ns + "number") }).ToList();

            //add root number
            ccidata[0].number = (string)doc.Descendants(nsControl + "control").First().Element(ns + "number");
            return ccidata;
        }
    }

}

Csharp相关问答推荐

如何在NServicebus中配置学习传输的文件夹(NService bus 8)

程序集.加载从exe的异常

如何将Kafka消息时间戳转换为C#中的日期和时间格式?

C#方法从AJAX调用接收NULL

C#EF Core WHERE IN LINQ FROM LIST WITH.CONTAINS不返回任何内容

如何在实体框架中添加包含列表?

C# CompareTo()和Compare()可以返回除-1和1以外的整数吗?

在C#中,非静态接口方法的抽象和虚拟是冗余的吗?

DbContext-传递自定义配置选项

为什么我可以用硬编码的集合而不是变量来设置没有setter的IList属性的值?

如何在GRPC代码First服务的返回类型上使用多态性?

为什么我的伺服电机不动,下面的代码?

用于请求用户返回列表的C#Google API

外部应用&&的LINQ;左外部连接&类似于PostgreSQL的查询

CsvHelper在第二次迭代时抛出System.ObjectDisposedException

LINQ在GROUP BY和JOIN之后获取子列表

从具有泛型类型约束的类继承-Blazor

自定义ConsoleForMatter中的DI/Http上下文

初始化具有EntityFrameworkCore和不同架构的数据库时引发System.MissingMethodExcept

在SQL中删除少于24小时的令牌