【问题标题】:Querying XMl with Indexed path使用索引路径查询 XML
【发布时间】:2015-10-14 00:26:14
【问题描述】:

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

所以我正在寻找的是能够使用类似这样的路径查询 xml 文件。

ReturnState[0]\ReturnDataState[0]\Form6[0]\Body[0]\Member[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>

谢谢, 阿杰

【问题讨论】:

  • 你的标题真的是要说 XMI(而不是 XML)吗?
  • 我只问 XMI 是一个有效的标签,如果它不是错字,那么你应该将标签添加到你的问题中。

标签: c# .net xml xml-parsing


【解决方案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>

【讨论】:

  • 感谢 Har07 的快速回复。真的很感激!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多