Home > Mobile >  XML Linq with nested elements
XML Linq with nested elements

Time:08-13

I'm trying to load an XML file formatted like this to a datatable:

<?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>

I'm able to pull family and other values, but when I try to access the statements I get nothing. I'd need to create a table with each of the statement values in a row and the parent values like family and title in the row as well.

This is far as I've gotten:

        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();

I'd appreciate any guidance.

CodePudding user response:

I think you will have much better luck using XPath. Try this:

// very crude resolver but should give you what's needed in this example
        class ControlsResolver : IXmlNamespaceResolver
        {
            public IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope)
            {
                return new Dictionary<string, string>() { { "controls", "http://scap.nist.gov/schema/sp800-53/feed/2.0" } };
            }

            public string LookupNamespace(string prefix)
            {
                return "http://scap.nist.gov/schema/sp800-53/feed/2.0";
            }

            public string LookupPrefix(string namespaceName)
            {
                return "controls";
            }
        }
//
//
//

var resolver = new ControlsResolver();

//this will give you all <controls:control> elements
var controlList = xdoc.XPathSelectElements("//controls:control", resolver);

//this just gets the children of the first one
var children = ele.First().Nodes();

// this gets any statements that are direct descendants but you'll have to iterate through them to find the other descendants
var statement = children.Where(n => ((XElement)n).Name.LocalName == "statement").ToList()

CodePudding user response:

I prefer using Xml Linq (XDocument). You are missing the namespaces. See code below :

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

}
  • Related