【发布时间】:2011-12-09 05:45:12
【问题描述】:
我有以下类型的 XML:
我希望通过这个 XML 填充以下 objectList。
List<Function> objFunctionsList = new List<Function>();
其中Function类如下,
public class Function
{
public String Name { get ; set; }
public Parameter ReturnType { get; set; }
public List<Parameter> Parameters { get; set; }
public String Library { get; set; }
public String Signature { get; set; }
public String Description { get; set; }
public String Code { get; set; }
}
而Parameter类如下,
public class Parameter
{
[DefaultValue("")]
public String Name { get; set; }
[DefaultValue("")]
public String DataType { get; set; }
[DefaultValue("")]
public String OccurenceType { get; set; }
}
您可以看到,在 XML 中,一些函数标签具有参数标签,而另一些则没有。我试过这个:
public const string XPATH_NAME = "/Functions/Function/Name";
public const string XPATH_LIBRARY = "/Functions/Function/Library";
public const string XPATH_SIGNATURE = "/Functions/Function/Signature";
public const string XPATH_DESCRIPTION = "/Functions/Function/Description";
public const string XPATH_CODE = "/Functions/Function/Code";
List<Function> objFunctionsList = new List<Function>();
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(pXMLPath);
XmlNodeList nlName = xmlDoc.SelectNodes(Constants.XPATH_NAME);
XmlNodeList nlLibrary = xmlDoc.SelectNodes(Constants.XPATH_LIBRARY);
XmlNodeList nlSignature = xmlDoc.SelectNodes(Constants.XPATH_SIGNATURE);
XmlNodeList nlDescription = xmlDoc.SelectNodes(Constants.XPATH_DESCRIPTION);
XmlNodeList nlCode = xmlDoc.SelectNodes(Constants.XPATH_CODE);
// Name, Signature, Library, element should be present in 'Function' node
if (nlName.Count == nlLibrary.Count
&& nlName.Count == nlSignature.Count
&& nlName.Count == nlDescription.Count
&& nlName.Count == nlCode.Count)
{
for (int iCount = 0; iCount < nlName.Count; iCount++)
{
Function objFunction = new Function();
objFunction.Name = nlName[iCount].InnerText.Trim();
objFunction.Library = nlLibrary[iCount].InnerText.Trim();
string signature = nlSignature[iCount].InnerText;
Parameter objReturnType = new Parameter();
string returnType = (nlSignature[iCount].Attributes[Constants.ATRR_TYPE] == null
? Constants.XSNOPARAM
: nlSignature[iCount].Attributes[Constants.ATRR_TYPE].Value);
if (returnType.EndsWith(Constants.ASTERIK))
{
objReturnType.DataType = returnType.Substring(0, returnType.Length - 1);
objReturnType.OccurenceType = Constants.OCCURENCES_ASTERISK;
}
else if (returnType.EndsWith(Constants.PLUS))
{
objReturnType.DataType = returnType.Substring(0, returnType.Length - 1);
objReturnType.OccurenceType = Constants.OCCURENCES_PLUS;
}
else if (returnType.EndsWith(Constants.QUESTION_MARK))
{
objReturnType.DataType = returnType.Substring(0, returnType.Length - 1);
objReturnType.OccurenceType = Constants.OCCURENCES_QUESTION;
}
else if (returnType.Length > 0)
{
objReturnType.DataType = returnType;
}
objFunction.ReturnType = objReturnType;
objFunction.Parameters = new List<Parameter>();
objFunction.Signature = signature;
objFunction.Description = nlDescription[iCount].InnerText.Trim();
objFunction.Code = nlCode[iCount].InnerText.Trim();
objFunctionsList.Add(objFunction);
}
}
}
但这是基于 XPath 的代码,并且在我之前在函数标签中没有参数标签时使用。
【问题讨论】:
-
我已经更新了我尝试过的代码
-
如果一个函数没有参数,那是否意味着它的
Parameters成员应该是null,或者是一个空列表,还是没有关系?
标签: c# .net xml linq xml-parsing