我无法正确地从XML查询数据。 问题是要从xml中获取正确的作者列表。
这是XML:
<b:Source xmlns:b=\"http://schemas.openxmlformats.org/officeDocument/2006/bibliography\">
<b:Tag>Paa15</b:Tag>
<b:SourceType>Book</b:SourceType>
<b:Guid>{A718F3A9-395B-40A0-8E01-71E033DF92ED}</b:Guid>
<b:Title>Test title</b:Title>
<b:Year>2015</b:Year>
<b:Publisher>PUB</b:Publisher>
<b:URL>http://www.ee</b:URL>
<b:DOI>123</b:DOI>
<b:Author>
<b:Author>
<b:NameList>
<b:Person>
<b:Last>LName</b:Last>
<b:Middle>MName</b:Middle>
<b:First>FName</b:First>
</b:Person>
<b:Person>
<b:Last>Smith</b:Last>
<b:First>John</b:First>
</b:Person>
</b:NameList>
</b:Author>
<b:Editor>
<b:NameList>
<b:Person>
<b:Last>Editor</b:Last>
<b:First>unknown</b:First>
</b:Person>
</b:NameList>
</b:Editor>
</b:Author>
<b:Pages>5</b:Pages>
<b:RefOrder>2</b:RefOrder>
</b:Source>
对象:
class Citation
{
public string TypeOfSource { get; set; }
public string Language { get; set; }
public List<Author> Author { get; set; }
public string Title { get; set; }
public string Year { get; set; }
public string Publisher { get; set; }
public string Editor { get; set; }
public string URL { get; set; }
public string DOI { get; set; }
}
class Author
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
}
代码:
List<Citation> citationList = new List<Citation>();
foreach (Source src in citations)
{
using (var xmlReader = new StringReader(src.XML))
{
var doc = XDocument.Load(xmlReader);
var result = (from cit in doc.Descendants()
where cit.Name.LocalName == "Source"
select new Citation
{
TypeOfSource = cit.Elements().Where(e => e.Name.LocalName == "SourceType").FirstOrDefault()?.Value ?? "",
Language = cit.Elements().Where(e => e.Name.LocalName == "Language").FirstOrDefault()?.Value ?? "",
Publisher = cit.Elements().Where(e => e.Name.LocalName == "Publisher").FirstOrDefault()?.Value ?? "",
Year = cit.Elements().Where(e => e.Name.LocalName == "Year").FirstOrDefault()?.Value ?? "",
Editor = cit.Elements().Where(e => e.Name.LocalName == "Editor").FirstOrDefault()?.Value ?? "",
URL = cit.Elements().Where(e => e.Name.LocalName == "URL").FirstOrDefault()?.Value ?? "",
DOI = cit.Elements().Where(e => e.Name.LocalName == "DOI").FirstOrDefault()?.Value ?? "",
Title = cit.Elements().Where(e => e.Name.LocalName == "Title").FirstOrDefault()?.Value ?? "",
Author = (from auth in doc.Descendants()
where auth.Name.LocalName == "Author"
select new Author
{
FirstName = auth.Elements().Where(e => e.Name.LocalName == "Author")
.Elements().Where(e => e.Name.LocalName == "NameList")
.Elements().Where(e => e.Name.LocalName == "Person")
.Elements().Where(e => e.Name.LocalName == "First").FirstOrDefault()?.Value ?? "",
LastName = auth.Elements().Where(e => e.Name.LocalName == "Author")
.Elements().Where(e => e.Name.LocalName == "NameList")
.Elements().Where(e => e.Name.LocalName == "Person")
.Elements().Where(e => e.Name.LocalName == "Last").FirstOrDefault()?.Value ?? "",
MiddleName = auth.Elements().Where(e => e.Name.LocalName == "Author")
.Elements().Where(e => e.Name.LocalName == "NameList")
.Elements().Where(e => e.Name.LocalName == "Person")
.Elements().Where(e => e.Name.LocalName == "Middle").FirstOrDefault()?.Value ?? "",
}).ToList()
}).FirstOrDefault();
citationList.Add(result);
}
正如我所说,选择作者列表有问题。我让两名作者进入列表,但第二位作者的值为空。而且,当我从XML中删除一个人和那个编辑器部分时,我仍然得到两个作者,其中第二个为空。有任何想法我在这里做错了吗?
也许以我在这里所做的方式从XML中选择作者不是最好的方法(最短的方法)?