Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Parse Optional or Null XML Elements & Attributes with LINQ?
    primarykey
    data
    text
    <p>I am having following type of XML:</p> <p><img src="https://i.stack.imgur.com/IczAG.png" alt="enter image description here"></p> <p>Through this XML I wish to populate following objectList.</p> <pre><code>List&lt;Function&gt; objFunctionsList = new List&lt;Function&gt;(); </code></pre> <p>where Function class is as follows,</p> <pre><code>public class Function { public String Name { get ; set; } public Parameter ReturnType { get; set; } public List&lt;Parameter&gt; Parameters { get; set; } public String Library { get; set; } public String Signature { get; set; } public String Description { get; set; } public String Code { get; set; } } </code></pre> <p>and the Parameter class is as follows,</p> <pre><code>public class Parameter { [DefaultValue("")] public String Name { get; set; } [DefaultValue("")] public String DataType { get; set; } [DefaultValue("")] public String OccurenceType { get; set; } } </code></pre> <p>You can see that in XML some function tags have Parameters tag while some others do not. I have tried this:</p> <pre><code>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&lt;Function&gt; objFunctionsList = new List&lt;Function&gt;(); 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 &amp;&amp; nlName.Count == nlSignature.Count &amp;&amp; nlName.Count == nlDescription.Count &amp;&amp; nlName.Count == nlCode.Count) { for (int iCount = 0; iCount &lt; 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 &gt; 0) { objReturnType.DataType = returnType; } objFunction.ReturnType = objReturnType; objFunction.Parameters = new List&lt;Parameter&gt;(); objFunction.Signature = signature; objFunction.Description = nlDescription[iCount].InnerText.Trim(); objFunction.Code = nlCode[iCount].InnerText.Trim(); objFunctionsList.Add(objFunction); } } } </code></pre> <p>but this is XPath based code and was in use earlier when I was not having Parameters Tag in the function tag.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload