我遇到的情况是我在字符串中有这样的xml:
string xmlString =
<parameters>
<parameter>
<name>name1</name>
<label>Max1</label>
<unit>Millions1</unit>
<component>
<type>Combo</type>
<attributes>
<type>Integer1</type>
<displayed>41</displayed>
<selected>01</selected>
<items>
<item>51</item>
<item>11</item>
<item>21</item>
<item>51</item>
</items>
</attributes>
</component>
</parameter>
<separator>Auto Skewing</separator>
<parameter>
<name>name2</name>
<label>Max2</label>
<unit>Millions2</unit>
<component>
<type>Combo</type>
<attributes>
<type>Integer2</type>
<displayed>42</displayed>
<selected>02</selected>
<items>
<item>5</item>
<item>12</item>
<item>22</item>
<item>52</item>
</items>
</attributes>
</component>
</parameter>
</parameters>
在这里,我必须使用Combo Bow渲染GUI(UIElements),如下所示:
首先如何序列化这个xml我只需要帮助参数类休息我可以自己做吗? 我可以通过这样做轻松地反序列化它:
XmlSerializer deserializer = new XmlSerializer(typeof(Parameters));
XmlReader reader = XmlReader.Create(new StringReader(xmlstring));
Parameters parameters = (Parameters)deserializer.Deserialize(reader);
但问题是在这一步之后我必须使用IValueConverter()的Converter()来渲染GUI,做这样的事情:
ComboBox cb = new ComboBox();
TextBox txtblk2 = new TextBox();
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ICollection<Parameter> list = value as ICollection<Parameter>;
List<UIElement> result = new List<UIElement>();
if (list != null)
{
foreach (Parameter p in list)
{
if (p.Component.Type == "Combo")
{
StackPanel st = new StackPanel()
{
Orientation = Orientation.Horizontal
};
var txtblk = new TextBox();
txtblk.Text = p.Label;
foreach (var item in p.Component.Attributes.Items)
{
cb.Items.Add(item); //These item contains a l0,15,45,60 to select through combo box
}
cb.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
cb.SelectedIndex = cb.Items.Count - 1;
txtblk2.Text = cb.SelectedValue.ToString() +" millions";
st.Children.Add(txtblk);
st.Children.Add(cb);
txtblk2.FontSize = 18;
st.Children.Add(txtblk2);
result.Add(st);
}
}
}
return(result);
}
现在的问题是如何绘制这个分隔线(我的xml中的自动偏斜),它将分离两个不同GUI的两个独立GUI的渲染(两个不同的&#34;参数&#34;存在于xml,请参阅快照以了解更多信息)?
答案 0 :(得分:1)
XmlSerializer
并不真正期望像这样的交错数据,即使您成功将数据序列化为对象模型,您也无法区分分隔符的位置。您最好的选择是将其加载到DOM(XmlElement
/ XElement
)中,然后循环遍历元素,或使用转换工具(如xslt)(尽管这不是很时髦这些天)。