给出以下示例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")
});
答案 0 :(得分:0)
试试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();
}
}
}