dinotom dinotom - 2 months ago 11
C# Question

Adding items to a list from within a query

Given the following sample xml

<?xml version="1.0" encoding="utf-8" ?>
<CodeSamples>
<CodeSample language="vba">
<!--all values in ModifierString,Parameters & ReturnType should be in lowercase-->
<MethodName>AddNewContact</MethodName>
<ModifierString value="public sub" />
<Parameters>
<Parameter parameterType ="string" parameterName ="wholeName" />
<Parameter parameterType ="string" parameterName ="email" />
<Parameter parameterType ="string" parameterName ="mobileNumber" />
</Parameters>
<ReturnType>void</ReturnType>
<CodeLines>
Dim outlookContact As Outlook.ContactItem
If outlookContact Is Nothing Then
Set outlookContact = Application.CreateItem(olContactItem)
With outlookContact
.fullName = wholeName
.Email1Address = email
.Email1DisplayName = wholeName
.MobileTelephoneNumber = mobileNumber
.Save
End With
End If
Set outlookContact = Nothing
</CodeLines>
</CodeSample>
</CodeSamples>


I am trying to build a query to return all instances of CodeSamples with a specific language, where I am stuck is how to get the list of parameters into the query. Here is the query so far, for now just trying to get out the one sample from the xml file.

Edited

var codeSample =
from element in xDoc.Element("CodeSamples")?.Elements("CodeSample")
where element.Attribute("language")?.Value == "vba" && element.Element("MethodName")?.Value == methodName
select new CodeSample()
{
Language = element.Attribute("language").Value,
Modifiers = element.Element("ModifierString")?.Value,
MethodName = methodName,
ReturnType = element.Element("ReturnType")?.Value,
CodeLines = element.Element("CodeLines")?.Value,
Parameters = element.Element("Parameters")?.Elements()
.Select(x => new MethodParameters
{
ParameterName = x.Attribute("parameterName").‌​Value,
ParameterType = x.Attribute("parameterType").Value
})
};


MethodParameters is a class

public class MethodParameters
{
public string ParameterName { get; set; }
public string ParameterType { get; set; }
}


Any help appreciated.

Final working Code thanks to jdweng

var codeSamples = xDoc.Descendants("CodeSample").Where(x => (string)x.Attribute("language") == language)
.Select(x => new CodeSample
{
Language = x.Attribute("language").Value,
Modifiers = x.Element("ModifierString")?.Value,
MethodName = x.Element("MethodName")?.Value,
ReturnType = x.Element("ReturnType")?.Value,
Parameters = x.Descendants("Parameter").Select(y => new MethodParameters {
ParameterType = (string)y.Attribute("parameterType"),
ParameterName = (string)y.Attribute("parameterName")
}).ToList(),
CodeLines = (string)x.Element("CodeLines")
});

Answer

Try xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("CodeSample").Where(x => (string)x.Attribute("language") == "vba").Select(x => new {
                parameters = x.Descendants("Parameter").Select(y => new {
                    parameterType = (string)y.Attribute("parameterType"),
                    parameterName = (string)y.Attribute("parameterName")
                }).ToList(),
                codeLines = (string)x.Element("CodeLines")
            }).ToList();

        }
    }
}