5

I'm trying to read string XML snippets from input using the XmlReader, manipulate the XDocument and output the result as a string.
In case there is a reference to unknown XML namespace, I just want this reference to be kept, not modified in any way.
Note I don't know the list of possible namespaces, so creating manually namespace whitelist is not an option.
I'm targeting the UWP platform, so XmlTextReader cannot be used here.

Sample of the input XML document:

<VisualState x:Name="Disabled">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

Sample of the expected output XML document:

<VisualState x:Name="Disabled">
  <VisualState.Setters>
    <Setter Target="RootGrid.Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
    <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
    <Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource SystemControlDisabledTransparentBrush}" />
  </VisualState.Setters>    
</VisualState>

My current code for parsing the XML:

using (StringReader sr = new StringReader(xml))
{
    using (XmlReader xtr = XmlReader.Create(sr))
    {
        return XDocument.Load(xtr);
    }
}
Martin Suchan
  • 10,600
  • 3
  • 36
  • 66
  • I assume the issue is this throws an exception? You're a little stuck here as the XML fragment isn't a valid XML document without the missing namespace declarations. `XmlTextReader` had a mode that turned off namespace support (and allowed a colon as a name character), but I don't think you'll find that anywhere else. – Charles Mager Mar 31 '17 at 14:02

1 Answers1

-1

Not ignoring them but simply accepting whenever one is present.

xml.Load(fil);
var ns = new XmlNamespaceManager(xml.NameTable);
var nsNode = xml.DocumentElement.Attributes.GetNamedItem("xmlns");
var nsurl = (nsNode != null) ? nsNode.Value : "";

ns.AddNamespace("ns", nsurl);

If you now prefix all your XPaths (if any) with "ns:" and pass the namespace manager as argument, you should be fine:

var nodeList = xml.SelectNodes("//ns:whatever", ns);

Edit: This is using XmlDocument, not XDocument, but should get you going too.

LocEngineer
  • 2,847
  • 1
  • 16
  • 28