Convert specific xml elements to datatable C#

285 views Asked by At

I have a powershell script that converts specific xml elements into a CSV. I would like to extend the functionality utilizing C#. the powershell script is as follows:

{
    [xml]$inp = get-content $infile
        $inp.config.devices.entry."device-group".entry |
        foreach {
            $vsys = $_.name
            $seq = 0   
            $_.$rulebase.security.rules.entry | 
                foreach { 
                    New-Object psobject -property @{
                    Vsys = $vsys ;
                    Seq = ++$seq;
                    Name = $_.name; 
                    From = fmat($_.from.member);
                    To = fmat($_.to.member);
                    Source = fmat($_.source.member);
                    Destination = fmat($_.destination.member);
                    SourceUser = fmat($_."source-user".member);
                    Category = fmat($_.category.member);
                    Application = fmat($_.application.member);
                    Service = fmat($_.service.member);
                    Action = $_.action;
                    NegateSource = $_."negate-source";
                    NegateDestination = $_."negate-destination"
                    Tag = fmat($_.tag.member); 
                    Target = fmat_vsys($_.target.devices) }  
                } 
        } | select Vsys, Seq, Name, From, Source, NegateSource, SourceUser, To, Destination, NegateDestination, Application, Service, Category, Action, Tag, Target | 
            export-csv $outfile
        Start-Process $outfile
    }

so far my C# looks like this:

try {
    var xdoc = XDocument.Load(filename);

    var results = xdoc.Elements("config")
        .Elements("devices")
        .Elements("entry")
        .Elements("vsys")
        .Elements("entry")
        .Elements("rulebase")
        .Elements("security")
        .Elements("rules")
        .Elements("entry")
        .Descendants()
        .Select(e => new {ElementName = e.Name, ElementValue = e.Value});

My question is, How can i get the data out of an anonymous object into a datatable so that my results are row based?

here is a sample of the XML

<rulebase>
            <security>
              <rules>
                <entry name="">
                  <from>
                    <member></member>
                  </from>
                  <to>
                    <member></member>
                  </to>
                  <source>
                    <member></member>
                  </source>
                  <destination>
                    <member></member>
                  </destination>
                  <source-user>
                    <member></member>
                  </source-user>
                  <category>
                    <member></member>
                  </category>
                  <application>
                    <member></member>
                    <member></member>
                    <member></member>
                    <member></member>
                  </application>
                  <service>
                    <member></member>
                  </service>
                  <hip-profiles>
                    <member></member>
                  </hip-profiles>
                  <action>deny</action>
                  <description></description>
                  <log-setting></log-setting>
                </entry>
                </rules>
                </security>
                </rulebase>
0

There are 0 answers