XML 在 XML 节点内
XML inside of XML Node
我 XML 如下所示:
<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>
然后我想存储在 属性
[XmlElement("StepXaml")]
public object StepXaml { get; set; }
我正在使用 XmlSerializer 将 XML 反序列化为包含 StepXaml 属性 的 class。目前,当我反序列化 XML 时,<StackPanel>
被反序列化到它自己的节点中。
有没有一种方法可以防止反序列化器试图深入 <StackPanel>
,而是将 <StepXaml>
和 </StepXaml>
之间的所有内容作为一个对象返回?
我不确定这是否是您想要的,但是如果您像这样为 CallStep
元素定义 class:
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; }
}
然后像这样调用反序列化:
//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);
}
然后 cs.StepXaml
将是一个 XmlDocument 包含以下内容:
<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>
我通过将 XAML 代码包装在 CDATA 块中解决了这个问题,如下所示:
<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>
然后我将其提取到我可以在 ContentControl
中使用的对象,如此
所示
我 XML 如下所示:
<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>
然后我想存储在 属性
[XmlElement("StepXaml")]
public object StepXaml { get; set; }
我正在使用 XmlSerializer 将 XML 反序列化为包含 StepXaml 属性 的 class。目前,当我反序列化 XML 时,<StackPanel>
被反序列化到它自己的节点中。
有没有一种方法可以防止反序列化器试图深入 <StackPanel>
,而是将 <StepXaml>
和 </StepXaml>
之间的所有内容作为一个对象返回?
我不确定这是否是您想要的,但是如果您像这样为 CallStep
元素定义 class:
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; }
}
然后像这样调用反序列化:
//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);
}
然后 cs.StepXaml
将是一个 XmlDocument 包含以下内容:
<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>
我通过将 XAML 代码包装在 CDATA 块中解决了这个问题,如下所示:
<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>
然后我将其提取到我可以在 ContentControl
中使用的对象,如此