使用LINQ的带有子查询的XML

3

我正在使用XML.Linq来搜索XML文件。

    <Location id="1284" parentID="1333" level="4" writerID="0" creatorID="3" nodeType="1266" template="1272" sortOrder="0" createDate="2012-04-09T09:41:33" updateDate="2012-08-16T11:43:11" nodeName="Princeton Junction" urlName="princeton-junction" writerName="Web" creatorName="My Name" path="-1,1092,1258,1333,1284" isDoc="">
      <solutionsOfferedIndustries>1109,1172,1290,1293,1295,1296,1298,1300,1302</solutionsOfferedIndustries>
      <umbracoUrlAlias>
      </umbracoUrlAlias>
      <uHidesy><![CDATA[{"document":{"id":1284,"documentType":"Location","tabs":[{"name":"Address & Contact Info","index":0,"properties":[{"name":"Map Position\u003cbr\u003e","index":0,"status":"show"},{"name":"Location Name","index":0,"status":"hide"},{"name":"Office Type\u003cbr\u003e","index":0,"status":"show"},{"name":"State (if in USA)","index":0,"status":"show"},{"name":"Street Address\u003cbr\u003e","index":0,"status":"show"},{"name":"Street Address (Line 2)","index":0,"status":"show"},{"name":"City\u003cbr\u003e","index":0,"status":"show"},{"name":"ZIP Code/Postal Code\u003cbr\u003e","index":0,"status":"show"},{"name":"Country","index":0,"status":"show"},{"name":"Location Phone Number","index":0,"status":"show"},{"name":"Location Fax Number","index":0,"status":"show"}],"status":"show"},{"name":"Building Photo","index":1,"properties":[{"name":"Upload Image\u003cbr\u003e","index":1,"status":"show"},{"name":"-Property-name-not-displayed-","index":1,"status":"show"}],"status":"show"},{"name":"Solutions Offered","index":2,"properties":[{"name":"Solutions Offered - Industries","index":2,"status":"show"}],"status":"show"},{"name":"Miscellaneous","index":3,"properties":[{"name":"Custom Page URL\u003cbr\u003e","index":3,"status":"hide"},{"name":"Divisions\u003cbr\u003e","index":3,"status":"show"},{"name":"General Manager","index":3,"status":"show"},{"name":"Notes\u003cbr\u003e","index":3,"status":"hide"}],"status":"show"},{"name":"Tab & Property Visibility","index":4,"properties":[{"name":"uHidesy\u003cbr\u003e","index":4,"status":"show"}],"status":"hide"},{"name":"Properties","index":5,"properties":[{"name":"Name","index":5,"status":"hide"},{"name":"Created by","index":5,"status":"hide"},{"name":"Created","index":5,"status":"hide"},{"name":"Id","index":5,"status":"hide"},{"name":"Document Type","index":5,"status":"hide"},{"name":"Template","index":5,"status":"hide"},{"name":"Publication Status","index":5,"status":"hide"},{"name":"Last edited","index":5,"status":"hide"},{"name":"Publish at","index":5,"status":"hide"},{"name":"Remove at","index":5,"status":"hide"},{"name":"Link to document","index":5,"status":"show"}],"status":"show"}],"applyToAll":true,"applyToAdmin":false,"savedDate":"16/08/2012 15:01:35"}}]]></uHidesy>
      <umbracoFile>/media/5153/front_of_building.jpg</umbracoFile>
      <coordinates><![CDATA[40.297833,-74.64424600000001,13]]></coordinates>
      <imageCrop>
        <crops date="10/05/2012 10:33:52">
          <crop name="Location Photo" x="287" y="0" x2="1785" y2="1498" url="/media/5153/front_of_building_Location Photo.jpg" />
        </crops>
      </imageCrop>
      <locationName>Princeton Junction</locationName>
      <officeType>Corporate Headquarters</officeType>
      <state><![CDATA[NJ - New Jersey]]></state>
      <streetAddress>123 My Road</streetAddress>
      <streetAddress2>
      </streetAddress2>
      <city>Princeton Junction</city>
      <zipCode>08550</zipCode>
      <country>United States</country>
      <divisions><![CDATA[Services,Products & Systems]]></divisions>
      <locationPhoneNumber>123-456-7890</locationPhoneNumber>
      <locationFaxNumber>123-456-7891</locationFaxNumber>
      <generalManager>
      </generalManager>
      <notes><![CDATA[Blah:
          -Blah1
          -Blah2
          -Blah3]]></notes>
    </Location>

我正在使用以下代码获取我需要的所有内容,但我还需要获取Location\imageCrop\crops\crop的属性url。
    var query = from c in xmldoc.Root.Descendants("Location")
                    select c.Attribute("id").ToString().Replace("\"", "").Replace("id=", "") + "\t"
                            + c.Element("locationName").Value + "\t"
                            + c.Element("streetAddress").Value + "\t"
                            + c.Element("streetAddress2").Value + "\t"
                            + c.Element("city").Value + "\t"
                            + c.Element("state").Value + "\t"
                            + c.Element("zipCode").Value + "\t"
                            + c.Element("country").Value + "\t"
                            + c.Element("locationPhoneNumber").Value + "\t"
                            + c.Element("locationFaxNumber").Value+ "\t"
                            + c.Attribute("urlName").ToString().Replace("\"", "").Replace("urlName=", "");

我该如何将子查询添加到这个查询中?
1个回答

3
你只需要:
c.Element("imageCrop").Element("crops").Element("crop").Attribute("url").Value

当然,如果有多个crop属性,那么它只会返回第一个-这对您来说可以吗?

(顺便说一句,我个人会将其转换为匿名类型,并将所有值存储在单独的属性中,然后将其转换为制表符分隔的值作为下一步;我认为您会发现这样做会使调试更容易。)


那很好用,谢谢你的附注。这是我第一次使用Linq和xml。 - John Harwood

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接