C# XML节点中的XML
我有如下XML:C# XML节点中的XML,c#,xml,xml-deserialization,C#,Xml,Xml Deserialization,我有如下XML: <CallStep> <StepXaml> <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:uc="clr-namespace:CallTracker.Library.UserControls.BaseUserControls;assembly=CallTracker.L
<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>
我使用XmlSerializer将XML反序列化为包含StepXaml属性的类。当前,当我反序列化XML时,正在反序列化到它自己的节点中
有没有一种方法可以防止反序列化程序尝试深入到,而是将所有介于和之间的内容作为一个对象返回?我不确定这是否是您想要的,但是如果您为CallStep元素定义了一个类,如下所示:
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);
}
然后按如下方式调用反序列化:
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将是一个包含以下内容的XML文档:
<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中使用该对象,如下图所示