I have 290 Group Policy Backup xml files which I need to enumerate in separate folders.
With each Group Policy backup xml file, I need to query the Policy settings.
Anyone who’s looked at a Group Policy xml backup file before would know they’re chock-a-block full of Namespace declarations.
I want to know, using Linq to XML, as I query each xml file, how can I dynamically query the XML the Namespace and then append the Namespace into the Linq query for the child nodes/values?
Here are some examples of the xml structure.
<User>
<ExtensionData>
<Extension xmlns:q1="http://www.microsoft.com/GroupPolicy/Settings/Scripts" xsi:type="q1:Scripts">
<q1:Script>
<ExtensionData>
<Extension xmlns:q1="http://www.microsoft.com/GroupPolicy/Settings/IE" xsi:type="q1:InternetExplorerSettings">
<q1:PreferenceMode>true</q1:PreferenceMode>
<ExtensionData>
<Extension xmlns:q2="http://www.microsoft.com/GroupPolicy/Settings/Registry" xsi:type="q2:RegistrySettings">
<q2:Policy>
<q2:Name>Disable changing accessibility settings</q2:Name>
<q2:State>Enabled</q2:State>
<ExtensionData>
<Extension xmlns:q1="http://www.microsoft.com/GroupPolicy/Settings/DriveMaps" xsi:type="q1:DriveMapSettings">
<q1:DriveMapSettings clsid="{8FDDCC1A-0C3C-43cd-A6B4-71A6DF20DA8C}">
My initial code looks like this:
Dim NS As XNamespace = "http://www.microsoft.com/GroupPolicy/Settings"
NodeValue = XDoc.Descendants(NS + NodeName).First().Value
As you can see I’m going to face literally dozens of different Namespaces, at this stage I don’t even know what they all are.
My end-task is to trawl through 290 directories, each containing one Group Policy xml backup file. I then need to read the Policy Name from each of the settings contained within the backup file.
Because I don’t know what Policy settings each xml flie will contain, I don’t know what Namespace(s) I need to use when attempting to read the xml values. Each xml file may even contain multiple namespaces.
How do I dynamically read the Namespace in Linq so I can read the values?
Thanks
Here’s what I ended up with: