1

I have XML like the following:

  <CallStep>
    <StepXaml>
      <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:uc="clr-namespace:CallTracker.Library.UserControls.BaseUserControls;assembly=CallTracker.Library">
        <uc:LabelValueControl Label="TestLabel" Value="356733" />
      </StackPanel>
    </StepXaml>
</CallStep>

that I would then like to store in a property

[XmlElement("StepXaml")]
public object StepXaml { get; set; }

I am using XmlSerializer to deserialize the XML into a class containing the StepXaml property. Currently when I deserialize the XML, the <StackPanel> is being deserialized into its own node.

Is there a way to prevent the deserializer from trying to drill down into <StackPanel>, but rather have everything between <StepXaml> & </StepXaml> returned as one object?

BrianKE
  • 4,035
  • 13
  • 65
  • 115

2 Answers2

0

I'm not sure if this is what you want, but if you define a class for your CallStep element like this:

public class CallStep
{
    //XmlElement attribute is not needed, because the name of the 
    //property and the XML element is the same
    public XmlDocument StepXaml { get; set; }
}

then call deserialization like this:

//xml is a string containing the XML from your question
XmlSerializer serializer = new XmlSerializer(typeof(CallStep));
using (StringReader reader = new StringReader(xml))
{
    CallStep cs = (CallStep)serializer.Deserialize(reader);
}

Then cs.StepXaml will be a XmlDocument containing this:

  <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:uc="clr-namespace:CallTracker.Library.UserControls.BaseUserControls;assembly=CallTracker.Library">
    <uc:LabelValueControl Label="TestLabel" Value="356733" />
  </StackPanel>
vesan
  • 3,289
  • 22
  • 35
0

I resolved this by wrapping the XAML code in a CDATA block like so:

    <StepXaml>
        <![CDATA[<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:uc="clr-namespace:CallTracker.Library.UserControls.BaseUserControls;assembly=CallTracker.Library">
            <uc:LabelValueControl Label="TestLabel2" Value="356738124315" />
          </StackPanel>]]>
    </StepXaml>

I then extract this to an object that I can use in a ContentControl as shown in this post

Community
  • 1
  • 1
BrianKE
  • 4,035
  • 13
  • 65
  • 115