使用索引路径查询XMl

时间:2015-10-14 00:26:14

标签: c# .net xml xml-parsing

我有一个xml文件,我想使用索引路径查询。我不确定它是否可能,但非常感谢任何帮助!

所以我要找的是能够用这样的路径查询xml文件。

ReturnState [0] \ ReturnDataState [0] \ Form6 [0] \体[0] \会员[0] \ FormA1

应该在第一个Member元素下给我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 个答案:

答案 0 :(得分:1)

Xpath拥有您需要遵循的自己的规范,它与您当前拥有的路径表达没有太大区别。这里重要的一些差异是,XPath索引从0开始而不是/,而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()); 。除非您乐意实现自己的解析器,否则最好稍微调整路径表达式以符合XPath语法:

<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>

<强> Dotnetfiddle Demo

输出

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(  )
{
  int i;
  int sales[30];
  int arraySize = 0;
  int temp[10] = { 0 };

  for ( i = 0; i < 30; i++ )
  {
    sales[i] = ( rand(  ) % 15000 ) + 1;    //   between 0 and 15000
  }

  printf( "Gross Sales of all 30 Salespeople\n" );

  for ( i = 0; i < 30; i++ )
  {
    printf( "%d\n", sales[i] ); //Displays Orginal List and Lists all values that were randomly selected
  }
  printf( "\nWage based on Gross Sales    \n" );    //Displays calculated wages with math equation


 // this does not do anything
//  for ( i = 0; i < 30; i++ )
//  {
//    printf( "%f\n", 100 + ( float ) sales[i] * 0.09 );    //Mathematical equation
//  }