我有一个xml文件,其结构如下:
<?xml version="1.0" encoding="UTF-8"?>
<EM>
<DEFAULT>
<Channel1>
<CONFIG_PARAM>
<Item name="line" value="0" />
<Item name="ULimt" value="0" />
<Item name="LLimit" value="0" />
<Item name="CLabel" value="0" />
</CONFIG_PARAM>
</Channel1>
<Channel2>
<CONFIG_PARAM>
<Item name="line" value="0" />
<Item name="ULimt" value="0" />
<Item name="LLimit" value="0" />
<Item name="CLabel" value="1" />
</CONFIG_PARAM>
</Channel2>
<CONFIG_PARAM>
<Item name="Frequency" value="50" />
<Item name="EMark" value="2" />
<Item name="Lang" value="LANG_ENG" />
<Item name="ID" value="111111111111111111111111111" />
<Item name="AMethod" value="AUC_CM_ERROR" />
<Item name="AValue" value="0" />
</CONFIG_PARAM>
</DEFAULT>
<UPDATED>
<Channel1>
<CONFIG_PARAM>
<Item name="line" value="0" />
<Item name="ULimt" value="0" />
<Item name="LLimit" value="0" />
<Item name="CLabel" value="0" />
</CONFIG_PARAM>
</Channel1>
<Channel2>
<CONFIG_PARAM>
<Item name="line" value="0" />
<Item name="ULimt" value="0" />
<Item name="LLimit" value="0" />
<Item name="CLabel" value="1" />
</CONFIG_PARAM>
</Channel2>
<CONFIG_PARAM>
<Item name="Frequency" value="50" />
<Item name="EMark" value="2" />
<Item name="Lang" value="LANG_ENG" />
<Item name="ID" value="111111111111111111111111111" />
<Item name="AMethod" value="AUC_CM_ERROR" />
<Item name="AValue" value="0" />
</CONFIG_PARAM>
</UPDATED>
</EM>
我有DEFAULT和UPDATED两个标签。在里面我有channel1,channel2和CONFIG_PARAM标签。 我的要求是这样的:
我想创建这样的属性:
default.Channel1
default.Channl2
default.Config_param
UPDATED标签的方式相同:
updated.Channel1
updated.Channel2
updated.config_param
例如像这样我想创建proprty:
private List<ParameterNode> channel1 = new List<ParameterNode>();
public List<ParameterNode> Channel1
{
get
{
return this.channel1;
}
}
请告诉我如何创建财产。
P.S:我写了这段代码来获得价值:
// find the channel1 element and load the Item
XmlElement channel1Element = GetUniqueElement(doc.DocumentElement, "Channel1");
if (channel1Element != null)
this.channel1 = ConfigParameterNode.LoadItems(channel1Element);
private static XmlElement GetUniqueElement(XmlNode parent, string name)
{
XmlElement result = null;
foreach (XmlNode node in parent.ChildNodes)
{
XmlElement element = node as XmlElement;
if (element == null)
continue;
if (element.Name != name)
continue;
result = element;
}
return result;
}
internal static List<ParameterNode> LoadItems(XmlElement parent)
{
List<ParameterNode> result = new List<ParameterNode>();
// go through all the children and find the Item elements
foreach (XmlNode child in parent.ChildNodes)
{
if (child.HasChildNodes)
{
foreach (XmlNode node in child.ChildNodes)
{
// is it an element
XmlElement element = node as XmlElement;
if (element == null)
continue;
// yes, so add it if it's an Item element
if (element.LocalName == "Item")
result.Add(new ParameterNode(element));
}
}
else
{
// is it an element
XmlElement element = child as XmlElement;
if (element == null)
continue;
// yes, so add it if it's an Item element
if (element.LocalName == "Item")
result.Add(new ParameterNode(element));
}
}
return result;
}
请告诉我如何更改我的代码,以便我可以创建属性default.Channel1,default.Channel2和default.config_param。
答案 0 :(得分:0)
我得到了这个问题的答案。
XmlElement defElement = GetUniqueElement(doc.DocumentElement,“DEFAULT”);
之后将此defElement传递给其他方法。我这个默认参数的属性。