dinotom dinotom - 1 year ago 87
C# Question

Adding items to a list from within a query

Given the following sample xml

<?xml version="1.0" encoding="utf-8" ?>
<CodeSample language="vba">
<!--all values in ModifierString,Parameters & ReturnType should be in lowercase-->
<ModifierString value="public sub" />
<Parameter parameterType ="string" parameterName ="wholeName" />
<Parameter parameterType ="string" parameterName ="email" />
<Parameter parameterType ="string" parameterName ="mobileNumber" />
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
End With
End If
Set outlookContact = Nothing

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.


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")
CodeLines = (string)x.Element("CodeLines")

Answer Source

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")
                codeLines = (string)x.Element("CodeLines")

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download