2015-10-14 74 views
0

我有一個xml文件,我想查詢使用索引路徑。我不確定這是否可能,但非常感謝任何幫助!使用索引路徑查詢XMl

所以我在找的是能夠用這樣的路徑查詢XML文件。

ReturnState [0] \ ReturnDataState [0] \ Form6 [0] \車身[0] \會員[0] \ FormA1

應該給我的第一個成員元素下的FormA1。這種方法有很多原因,並且沒有深入細節,我想知道是否可以使用xpath或其他方式查詢這樣的內容。

<ReturnState> 
    <ReturnDataState> 
    <Form6>  
     <Body>  
     <Member> 
      <MemberName> 
      <BusinessNameLine1Txt>Mouser0</BusinessNameLine1Txt> 
      </MemberName> 
      <FormA1> 
      <PartI-SalesFactor> 
       <SalesDelOrShippedOutState>31754631</SalesDelOrShippedOutState> 
       <TotalSales> 
       <Wisconsin>31754631</Wisconsin> 
       <TotalCompany>1965873635</TotalCompany> 
       </TotalSales> 
       <SalesFactorTotal> 
       <Wisconsin>31754631</Wisconsin> 
       <TotalCompany>1965873635</TotalCompany> 
       </SalesFactorTotal> 
       <ApportionmentPercentage>0.000000</ApportionmentPercentage> 
      </PartI-SalesFactor> 
      </FormA1> 
     </Member> 
     <Member> 
      <MemberName> 
      <BusinessNameLine1Txt>Mouser1</BusinessNameLine1Txt> 
      </MemberName> 
      <FormA1> 
      <PartI-SalesFactor> 
       <SalesDelOrShippedOutState>31754632</SalesDelOrShippedOutState> 
       <TotalSales> 
       <Wisconsin>31754632</Wisconsin> 
       <TotalCompany>1965873633</TotalCompany> 
       </TotalSales> 
       <SalesFactorTotal> 
       <Wisconsin>31754632</Wisconsin> 
       <TotalCompany>196587344</TotalCompany> 
       </SalesFactorTotal> 
       <ApportionmentPercentage>1.000000</ApportionmentPercentage> 
      </PartI-SalesFactor> 
      </FormA1> 
     </Member> 
     <Member> 
       <MemberName> 
       <BusinessNameLine1Txt>Mouser2</BusinessNameLine1Txt> 
       </MemberName> 
       <FormA1> 
       <PartI-SalesFactor> 
        <SalesDelOrShippedOutState>31754632</SalesDelOrShippedOutState> 
        <TotalSales> 
        <Wisconsin>31754632</Wisconsin> 
        <TotalCompany>1965873633</TotalCompany> 
        </TotalSales> 
        <SalesFactorTotal> 
        <Wisconsin>31754632</Wisconsin> 
        <TotalCompany>196587344</TotalCompany> 
        </SalesFactorTotal> 
        <ApportionmentPercentage>1.000000</ApportionmentPercentage> 
       </PartI-SalesFactor> 
      </FormA1> 
     </Member> 
     <Member> 
      <MemberName> 
      <BusinessNameLine1Txt>Mouser3</BusinessNameLine1Txt> 
      </MemberName> 
      <FormA1> 
      <PartI-SalesFactor> 
       <SalesDelOrShippedOutState>31754632</SalesDelOrShippedOutState> 
       <TotalSales> 
       <Wisconsin>31754632</Wisconsin> 
       <TotalCompany>1965873633</TotalCompany> 
       </TotalSales> 
       <SalesFactorTotal> 
       <Wisconsin>31754632</Wisconsin> 
       <TotalCompany>196587344</TotalCompany> 
       </SalesFactorTotal> 
       <ApportionmentPercentage>1.000000</ApportionmentPercentage> 
      </PartI-SalesFactor> 
      </FormA1> 
     </Member> 
    </Body> 
    </Form6> 
    </ReturnDataState> 
</ReturnState> 

感謝, AJ

+1

你真的認爲你的標題說XMI(而不是XML)? – DeanOC

+0

我只問XMI是一個有效的標籤,如果它不是一個錯字,那麼你應該添加標籤到你的問題。 – DeanOC

回答

1

的Xpath有它自己的規範,你需要遵循,這是不是從你目前擁有的路徑表達式太大的不同。這裏重要的一些差異是,XPath索引從1開始不是0,並且XPath中的路徑分隔符是/而不是\。更好地稍微調整你的路徑表達式符合XPath語法,除非你是快樂的實現自己的解析器:

var doc = XDocument.Load("path_to_your_file.xml"); 
var xpath = "ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1"; 
var result = doc.XPathSelectElement(xpath); 
Console.WriteLine(result.ToString()); 

Dotnetfiddle Demo

輸出:

<FormA1> 
    <PartI-SalesFactor> 
    <SalesDelOrShippedOutState>31754631</SalesDelOrShippedOutState> 
    <TotalSales> 
     <Wisconsin>31754631</Wisconsin> 
     <TotalCompany>1965873635</TotalCompany> 
    </TotalSales> 
    <SalesFactorTotal> 
     <Wisconsin>31754631</Wisconsin> 
     <TotalCompany>1965873635</TotalCompany> 
    </SalesFactorTotal> 
    <ApportionmentPercentage>0.000000</ApportionmentPercentage> 
    </PartI-SalesFactor> 
</FormA1> 
+0

感謝Har07的快速回復。真的很感激它! – user3375390