2012-10-12 3 views
3

XML 파일을 통해 XML.Linq를 검색하고 있습니다.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> 

내가 필요한 모든 것을 얻을 수있는 다음 코드를 사용하고 있지만, 나는 또한 위치 \ imageCrop \ 작물 \ 작물의 속성 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=", ""); 

어떻게이 하위 쿼리를이 쿼리에 추가합니까? 여러 crop 속성이있는 경우 그 첫 번째 명령 만 반환합니다, 물론

c.Element("imageCrop").Element("crops").Element("crop").Attribute("url").Value 

- 당신을 위해 괜찮 :

답변

3

당신은해야합니까?

(제쳐두고 개인적으로 모든 값을 별도의 속성에 저장 한 익명 형식으로 변환 한 다음 탭으로 구분 된 값으로 변환하여 다음 단계로 사용하게 될 것이라고 생각합니다. 디버깅이 더 쉬워졌습니다.)

+0

위 기능에 도움이되었습니다. Linq와 xml로 작업 한 것은 이번이 처음입니다. –

관련 문제