从查询中向列表中添加项
本文关键字:添加 列表 查询 | 更新日期: 2023-09-27 18:08:16
给定以下示例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>
我正试图构建一个查询,以返回具有特定语言的CodeSamples的所有实例,我被卡住的地方是如何将参数列表输入查询。下面是到目前为止的查询,目前只是尝试从xml文件中取出一个示例。
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是一个类
public class MethodParameters
{
public string ParameterName { get; set; }
public string ParameterType { get; set; }
}
感谢您的帮助。
最终工作代码感谢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")
});
尝试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();
}
}
}