我有以下类型的XML:
I am having following type of XML:
通过这个XML我想填充以下链表。
Through this XML I wish to populate following objectList.
List<Function> objFunctionsList = new List<Function>();
,其中Function类的有:
where Function class is as follows,
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; }
}
和参数类,如下所示,
public class Parameter
{
[DefaultValue("")]
public String Name { get; set; }
[DefaultValue("")]
public String DataType { get; set; }
[DefaultValue("")]
public String OccurenceType { get; set; }
}
您可以看到,在一些XML标签功能有参数的标签,而有些则没有。我曾尝试这样的:
You can see that in XML some function tags have Parameters tag while some others do not. I have tried this:
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的code和是在使用更早的时候,我没有在函数变量参数标签。
but this is XPath based code and was in use earlier when I was not having Parameters Tag in the function tag.
检查,看看是否这会有所帮助 - http://blogs.msdn.com/b/ericwhite/archive/2009/05/14/working-with-optional-elements-and-attributes-in-linq-to-xml-queries.aspx
Check this and see if this helps - http://blogs.msdn.com/b/ericwhite/archive/2009/05/14/working-with-optional-elements-and-attributes-in-linq-to-xml-queries.aspx