XDocument docs = XDocument.Load(new StreamReader(response.GetResponseStream()));
List<string> books = docs.Descendants("INTEL")
.Select(x => new {
Title = (string) x.Element("TITLE"),
Author = x.Element("INTEL_AUTH")
})
.Select(x => new {
Title = x.Title,
FirstName = (string) x.Author.Element("FNAME"),
MiddleInitial = (string) x.Author.Element("MNAME"),
LastName = (string) x.Author.Element("LNAME"),
})
.Select(x => string.Format("{0}: {1} {2} {3}",
x.Title,
x.FirstName, x.MiddleInitial, x.LastName))
.ToList();
<Data xmlns="http://www.TestWebsite.com/schema/data" xmlns:dmd="http://www.TestWebsite.com/schema/data-metadata" dmd:date="2012-03-12">
<Record userId="62972" username="j23" termId="99" dmd:surveyId="2144185">
<dmd:IndexEntry indexKey="COLLEGE" entryKey="Business" text="Business"></dmd:IndexEntry>
<dmd:IndexEntry indexKey="DEPARTMENT" entryKey="Business/Finance" text="Business/Finance"></dmd:IndexEntry>
<INTEL id="20" dmd:lastModified="2012-02-09T13:01:09">
<STATUS>Published</STATUS>
<TITLE> House Price Indices</TITLE>
<VOLUME>32</VOLUME>
<ISSUE>1</ISSUE>
<PAGENUM>127-160</PAGENUM>
<CONTYPE>Journal Article/Notes/Comments</CONTYPE>
<SCALE></SCALE>
<REFEREED>Refereed</REFEREED>
<CLASSIFICATION>Discipline-based Scholarship</CLASSIFICATION>
<INTELLCONT_AUTH id="20327331841">
<FNAME>John</FNAME>
<MNAME>M.</MNAME>
<LNAME>Doe</LNAME>
</INTELLCONT_AUTH>
<PUBLISHER>Real Estate Economics</PUBLISHER>
<AUDIENCE>International</AUDIENCE>
<PUBLICAVAIL>Yes</PUBLICAVAIL>
<PUBLICATION_CODE>Basic Research</PUBLICATION_CODE>
<USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR>
</INTELLCONT>
<INTELLCONT id="20327360512" dmd:lastModified="2012-02-09T13:01:09" dmd:startDate="2011-12-01" dmd:endDate="2011-12-31">
<STATUS>In Prep Not Yet Submitted</STATUS>
<TITLE>Time on the Market over the Cycle: a Spatial Model</TITLE>
<VOLUME></VOLUME>
<ISSUE></ISSUE>
<PAGENUM></PAGENUM>
<CONTYPE>Journal Article/Notes/Comments</CONTYPE>
<CONTYPEOTHER></CONTYPEOTHER>
<SCALE></SCALE>
<REFEREED>Refereed</REFEREED>
<COPYRIGHT></COPYRIGHT>
<CLASSIFICATION>Discipline-based Scholarship</CLASSIFICATION>
<MERIT>Journal List</MERIT>
<INTEL_AUTH id="13">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME>62972</FACULTY_NAME>
<FNAME>John</FNAME>
<MNAME>M.</MNAME>
<LNAME>Doe</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="20327360514">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME></FACULTY_NAME>
<FNAME>Erik</FNAME>
<MNAME></MNAME>
<LNAME>deWit</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="5">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME></FACULTY_NAME>
<FNAME>Xiao-Song</FNAME>
<MNAME></MNAME>
<LNAME>Liu</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<PUBLISHER></PUBLISHER>
<PUBCTYST></PUBCTYST>
<PUBCNTRY></PUBCNTRY>
<WEB_ADDRESS></WEB_ADDRESS>
<EDITORS></EDITORS>
<ISBNISSN></ISBNISSN>
<AGENCY></AGENCY>
<AUDIENCE>International</AUDIENCE>
<PUBLICAVAIL></PUBLICAVAIL>
<SEA_GRANT></SEA_GRANT>
<SEA_GRANT_PROJECTNUM></SEA_GRANT_PROJECTNUM>
<PUBLICATION_CODE>Basic Research</PUBLICATION_CODE>
<ABSTRACT></ABSTRACT>
<REVIEW id="20327360516">
<CITATION></CITATION>
</REVIEW>
<FULL_TEXT></FULL_TEXT>
<USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR>
<DTM_EXPSUB>December</DTM_EXPSUB>
<DTD_EXPSUB></DTD_EXPSUB>
<DTY_EXPSUB>2011</DTY_EXPSUB>
<EXPSUB_START>2011-12-01</EXPSUB_START>
<EXPSUB_END>2011-12-31</EXPSUB_END>
<DTM_SUB></DTM_SUB>
<DTD_SUB></DTD_SUB>
<DTY_SUB></DTY_SUB>
<SUB_START></SUB_START>
<SUB_END></SUB_END>
<DTM_ACC></DTM_ACC>
<DTD_ACC></DTD_ACC>
<DTY_ACC></DTY_ACC>
<ACC_START></ACC_START>
<ACC_END></ACC_END>
<DTM_PUB></DTM_PUB>
<DTD_PUB></DTD_PUB>
<DTY_PUB></DTY_PUB>
<PUB_START></PUB_START>
<PUB_END></PUB_END>
</INTEL>
<INTEL id="243" dmd:lastModified="2012-02-09T13:01:09" dmd:startDate="2011-04-01" dmd:endDate="2011-04-30">
<STATUS>Submitted/Under Review</STATUS>
<TITLE> shopping centers</TITLE>
<VOLUME></VOLUME>
<ISSUE></ISSUE>
<PAGENUM></PAGENUM>
<CONTYPE>Journal Article/Notes/Comments</CONTYPE>
<CONTYPEOTHER></CONTYPEOTHER>
<SCALE></SCALE>
<REFEREED>Refereed</REFEREED>
<ORIG_RESEARCH></ORIG_RESEARCH>
<COPYRIGHT></COPYRIGHT>
<CLASSIFICATION>Discipline-based Scholarship</CLASSIFICATION>
<MERIT></MERIT>
<INTEL_AUTH id="24223270914">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME>62972</FACULTY_NAME>
<FNAME>John</FNAME>
<MNAME>M.</MNAME>
<LNAME>Doe</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="16">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME>89176</FACULTY_NAME>
<FNAME>yna</FNAME>
<MNAME></MNAME>
<LNAME>vei</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="917">
<STUDENT_LEVEL>Graduate</STUDENT_LEVEL>
<FACULTY_NAME></FACULTY_NAME>
<FNAME>gyu</FNAME>
<MNAME></MNAME>
<LNAME>Zhou</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<PUBLISHER>JREFE</PUBLISHER>
<PUBCTYST></PUBCTYST>
<PUBCNTRY></PUBCNTRY>
<WEB_ADDRESS></WEB_ADDRESS>
<EDITORS></EDITORS>
<ISBNISSN></ISBNISSN>
<AGENCY></AGENCY>
<AUDIENCE></AUDIENCE>
<PUBLICAVAIL></PUBLICAVAIL>
<SEA_GRANT></SEA_GRANT>
<SEA_GRANT_PROJECTNUM></SEA_GRANT_PROJECTNUM>
<PUBLICATION_CODE></PUBLICATION_CODE>
<DESC></DESC>
<ABSTRACT>of competing space.
是
<DTD_EXPSUB></DTD_EXPSUB>
<DTY_EXPSUB></DTY_EXPSUB>
<EXPSUB_START></EXPSUB_START>
<EXPSUB_END></EXPSUB_END>
<DTM_SUB>April</DTM_SUB>
<DTD_SUB></DTD_SUB>
<DTY_SUB>2011</DTY_SUB>
<SUB_START>2011-04-01</SUB_START>
<SUB_END>2011-04-30</SUB_END>
<DTM_ACC></DTM_ACC>
<DTD_ACC></DTD_ACC>
<DTY_ACC></DTY_ACC>
<ACC_START></ACC_START>
<ACC_END></ACC_END>
<DTM_PUB></DTM_PUB>
<DTD_PUB></DTD_PUB>
<DTY_PUB></DTY_PUB>
<PUB_START></PUB_START>
<PUB_END></PUB_END>
</INTEL>
<INTEL id="24" dmd:lastModified="2012-02-09T13:01:09" dmd:startDate="2011-04-01" dmd:endDate="2011-04-30">
<STATUS>Submitted/Under Review</STATUS>
<TITLE> House Prices</TITLE>
<VOLUME></VOLUME>
<ISSUE></ISSUE>
<PAGENUM></PAGENUM>
<CONTYPE>Journal Article/Notes/Comments</CONTYPE>
<CONTYPEOTHER></CONTYPEOTHER>
<SCALE></SCALE>
<REFEREED>Refereed</REFEREED>
<ORIG_RESEARCH></ORIG_RESEARCH>
<COPYRIGHT></COPYRIGHT>
<CLASSIFICATION>Discipline-based Scholarship</CLASSIFICATION>
<MERIT>Journal List</MERIT>
<INTEL_AUTH id="225">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME>62972</FACULTY_NAME>
<FNAME>John</FNAME>
<MNAME>M.</MNAME>
<LNAME>aDoe</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="20327348226">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME></FACULTY_NAME>
<FNAME>Piet</FNAME>
<MNAME></MNAME>
<LNAME>Eichholtz</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="20327348227">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME></FACULTY_NAME>
<FNAME>Thies</FNAME>
<MNAME></MNAME>
<LNAME>Lindenthal</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<PUBLISHER>AER</PUBLISHER>
<PUBCTYST></PUBCTYST>
<PUBCNTRY></PUBCNTRY>
<WEB_ADDRESS></WEB_ADDRESS>
<EDITORS></EDITORS>
<ISBNISSN></ISBNISSN>
<AGENCY></AGENCY>
<AUDIENCE>International</AUDIENCE>
<PUBLICAVAIL>Yes</PUBLICAVAIL>
<SEA_GRANT></SEA_GRANT>
<SEA_GRANT_PROJECTNUM></SEA_GRANT_PROJECTNUM>
<PUBLICATION_CODE></PUBLICATION_CODE>
<DESC></DESC>
<ABSTRACT></ABSTRACT>
<REVIEW id="20327348228">
<CITATION></CITATION>
</REVIEW>
<FULL_TEXT></FULL_TEXT>
<USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR>
<DTM_EXPSUB></DTM_EXPSUB>
<DTD_EXPSUB></DTD_EXPSUB>
<DTY_EXPSUB></DTY_EXPSUB>
<EXPSUB_START></EXPSUB_START>
<EXPSUB_END></EXPSUB_END>
<DTM_SUB>April</DTM_SUB>
<DTD_SUB></DTD_SUB>
<DTY_SUB>2011</DTY_SUB>
<SUB_START>2011-04-01</SUB_START>
<SUB_END>2011-04-30</SUB_END>
<DTM_ACC></DTM_ACC>
<DTD_ACC></DTD_ACC>
<DTY_ACC></DTY_ACC>
<ACC_START></ACC_START>
<ACC_END></ACC_END>
<DTM_PUB></DTM_PUB>
<DTD_PUB></DTD_PUB>
<DTY_PUB></DTY_PUB>
<PUB_START></PUB_START>
<PUB_END></PUB_END>
</INTEL>
<INTEL id="280" dmd:lastModified="2011-05-24T15:31:40" dmd:startDate="2011-04-21" dmd:endDate="2011-04-21">
<STATUS>Revising to Resubmit</STATUS>
<TITLE>Hedonic Analysis of Price Elasticity of Demand: The Case of New Condominiums in Shenzhen, China </TITLE>
<VOLUME></VOLUME>
<ISSUE></ISSUE>
<PAGENUM></PAGENUM>
<CONTYPE>Other</CONTYPE>
<CONTYPEOTHER>Manuscript</CONTYPEOTHER>
<ORIG_RESEARCH></ORIG_RESEARCH>
<COPYRIGHT></COPYRIGHT>
<CLASSIFICATION>Discipline-based Scholarship</CLASSIFICATION>
<MERIT>Journal List</MERIT>
<INTEL id="20327342081">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME>62972</FACULTY_NAME>
<FNAME>John</FNAME>
<MNAME>M.</MNAME>
<LNAME>Doe</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="20327342082">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME></FACULTY_NAME>
<FNAME>Yong</FNAME>
<MNAME></MNAME>
<LNAME>Chen</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="20327342083">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME></FACULTY_NAME>
<FNAME>Dogan</FNAME>
<MNAME></MNAME>
<LNAME>Tirtiroglu</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<PUBLISHER>Journal of Housing Economics</PUBLISHER>
<PUBCTYST></PUBCTYST>
<PUBCNTRY>The Netherlands</PUBCNTRY>
<WEB_ADDRESS></WEB_ADDRESS>
<EDITORS></EDITORS>
<ISBNISSN></ISBNISSN>
<AGENCY></AGENCY>
<AUDIENCE>International</AUDIENCE>
<PUBLICAVAIL>Yes</PUBLICAVAIL>
<SEA_GRANT></SEA_GRANT>
<SEA_GRANT_PROJECTNUM></SEA_GRANT_PROJECTNUM>
<PUBLICATION_CODE>Basic Research</PUBLICATION_CODE>
<DESC></DESC>
<ABSTRACT></ABSTRACT>
<REVIEW id="20327342084">
<CITATION></CITATION>
</REVIEW>
<FULL_TEXT></FULL_TEXT>
<USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR>
<DTM_EXPSUB></DTM_EXPSUB>
<DTD_EXPSUB></DTD_EXPSUB>
<DTY_EXPSUB></DTY_EXPSUB>
<EXPSUB_START></EXPSUB_START>
<EXPSUB_END></EXPSUB_END>
<DTM_SUB>April</DTM_SUB>
<DTD_SUB>21</DTD_SUB>
<DTY_SUB>2011</DTY_SUB>
<SUB_START>2011-04-21</SUB_START>
<SUB_END>2011-04-21</SUB_END>
<DTM_ACC></DTM_ACC>
<DTD_ACC></DTD_ACC>
<DTY_ACC></DTY_ACC>
<ACC_START></ACC_START>
<ACC_END></ACC_END>
<DTM_PUB></DTM_PUB>
<DTD_PUB></DTD_PUB>
<DTY_PUB></DTY_PUB>
<PUB_START></PUB_START>
<PUB_END></PUB_END>
</INTEL>
<INTEL id="20327340032" dmd:lastModified="2012-02-09T13:01:09" dmd:startDate="2011-03-01" dmd:endDate="2011-03-31">
<STATUS>Submitted/Under Review</STATUS>
<TITLE>Empirical estimation of the option premium for residential redevelopment </TITLE>
<VOLUME></VOLUME>
<ISSUE></ISSUE>
<PAGENUM></PAGENUM>
<CONTYPE>Journal Article/Notes/Comments</CONTYPE>
<CONTYPEOTHER></CONTYPEOTHER>
<SCALE></SCALE>
<REFEREED>Refereed</REFEREED>
<ORIG_RESEARCH></ORIG_RESEARCH>
<COPYRIGHT></COPYRIGHT>
<CLASSIFICATION>Discipline-based Scholarship</CLASSIFICATION>
<MERIT>Journal List</MERIT>
<INTEL_AUTH id="20327340033">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME>62972</FACULTY_NAME>
<FNAME>John</FNAME>
<MNAME>M.</MNAME>
<LNAME>Doe</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="20327340034">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME></FACULTY_NAME>
<FNAME>Katsiaryna </FNAME>
<MNAME></MNAME>
<LNAME>Salavei</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<INTEL_AUTH id="20327340035">
<STUDENT_LEVEL></STUDENT_LEVEL>
<FACULTY_NAME></FACULTY_NAME>
<FNAME>S. K. </FNAME>
<MNAME></MNAME>
<LNAME>Wong</LNAME>
<INSTITUTION></INSTITUTION>
</INTEL_AUTH>
<PUBLISHER>RSUE</PUBLISHER>
<PUBCTYST></PUBCTYST>
<PUBCNTRY></PUBCNTRY>
-01-01</PUB_START>
<PUB_END>2004-12-31</PUB_END>
</INTEL>
</Record>
</Data>
我添加了部分加载到doc.Load(响应)部分的XML。请检查并
我添加了部分加载到doc.Load(响应)部分的XML。请检查并
我添加了部分加载到doc.Load(响应)部分的XML。请检查并
答案 0 :(得分:4)
你确定外壳是正确的,因为我确定XDocument是区分大小写的,在你的代码示例中你有“Intel”,你在下面的评论中有“INTEL”吗?
修改强>
好的,它可能是另一件事,这让我在过去就是命名空间声明。
检查此question的答案,看看是否有帮助。
修改强>
使用您的XML我可以执行以下操作:
XDocument doc = XDocument.Load(@"C:\Temp\Test.xml");
XName name = XName.Get("INTEL", "http://www.TestWebsite.com/schema/data");
var nodes = doc.Root.Descendants(name);
foreach (var node in nodes)
{
Console.WriteLine(node.Value);
}
Console.ReadLine();
并打印每个“INTEL”节点的值。但是,我确实需要修改xml以使其工作,我猜测因为你的XML实际上相当大,所以关闭标签有一些问题所以我关闭了那些然后XDocument可以加载文件。
如果你使用类似的代码返回后代,我不太清楚为什么你没有得到任何结果,看来你原来的问题文本已经消失了:(。
答案 1 :(得分:1)
//List<string> books = docs.Descendants("INTEL")
XNamespace ns = @"http://www.TestWebsite.com/schema/data";
List<string> books = docs.Descendants(ns + "INTEL")
答案 2 :(得分:1)
您问题中的代码不使用命名空间。你说你现在已经尝试过了,但是由于你没有用新代码更新问题,所以你已经尝试了什么。
我更喜欢使用XNamespace
类型和+
运算符,而不是调用XName.Get
,但基本上两种方式都应该有效。这是一个简短但完整的程序显示工作:
using System;
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load("test.xml");
XNamespace ns = "http://www.TestWebsite.com/schema/data";
var query = doc.Descendants(ns + "INTEL");
Console.WriteLine(query.Count()); // Prints 1
}
}
XML(只是问题内容的缩减版):
<Data xmlns="http://www.TestWebsite.com/schema/data"
xmlns:dmd="http://www.TestWebsite.com/schema/data-metadata"
dmd:date="2012-03-12">
<Record userId="62972" username="j23" termId="99" dmd:surveyId="2144185">
<dmd:IndexEntry indexKey="COLLEGE" entryKey="Business" text="Business">
</dmd:IndexEntry>
<INTEL id="20" dmd:lastModified="2012-02-09T13:01:09" />
</Record>
</Data>