2015-10-14 88 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