在C#中读取Mulitple子项并提取数据xmlReader [英] Read Mulitple childs and extract data xmlReader in c#

查看:35
本文介绍了在C#中读取Mulitple子项并提取数据xmlReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML:

<InformationTuples>
      <InformationTuple>
       <Name>documentClass</Name>
        <value format="" valueset="{rechnung}" originalValue="Rechnung" start="0" end="0" LD="0" doc="C:\b4enviam-service-test\inputDir\031a0933-2616-4d8e-8a79-56746ae0e160/Invoice_51029062.pdf">Rechnung</value>
        <EntityType>Class</EntityType>
        <state>New       </state>
        <need>Mandatory </need>
        <extractionmethod>
        </extractionmethod>
        <weight>1</weight>
        <precondition type="optional">All</precondition>
      </InformationTuple>
      <InformationTuple>
        <Name>SAPNr.</Name>
        <value format="" valueset="" originalValue="4352020616" start="0" end="0" LD="0" doc="C:\b4enviam-service-test\inputDir\031a0933-2616-4d8e-8a79-56746ae0e160/Invoice_51029062.pdf">4352020616</value>
        <EntityType>KB.GTInovice</EntityType>
        <state>New       </state>
        <need>Mandatory </need>
        <extractionmethod>
        </extractionmethod>
        <weight>1</weight>
        <precondition type="optional">all</precondition>
      </InformationTuple>
      <InformationTuple>
        <Name>GT-Invoice</Name>
        <value format="" valueset="" originalValue="" start="0" end="0" LD="0" doc="">
        </value>
        <EntityType>KB.GTInovice</EntityType>
        <state>New       </state>
        <need>Mandatory </need>
        <extractionmethod>
        </extractionmethod>
        <weight>1</weight>
        <precondition type="optional">all</precondition>
      </InformationTuple>
    </InformationTuples>

C#

reader.ReadToFollowing("InformationTuple");
                   reader2.ReadToFollowing("InformationTuple");



                   do
                   {
                       subtree = reader2.ReadSubtree();
                       subtree.ReadToFollowing("Name");
                       Debug.WriteLine(subtree.ReadElementContentAsString());
                       reader2.ReadToNextSibling("InfromationTuple");



                   } while (reader.ReadToNextSibling("InformationTuple"))

我现在尝试了一段时间,使用c#从XML的多个子对象中提取数据,但是没有成功.我尝试了多个代码段,但无法提取数据.

I'm trying for a while now to extract data from multiple childs in XML using c# but didn't successful. I have tried multiple code snippets but unable to extract data.

就像我必须提取三个信息元组中给出的数据一样,但是XMLreader中给出的功能在单循环迭代后无法正常运行,但读者指针中断了(无法移至第二个InformationTuple),即使我尝试了两个不同的读者指针,但它现在给例外.

Like i have to extract the data given in three information tuples, but functions given in the XMLreader not working properly reader pointer break after single loop iteration (unable to move to second InformationTuple), even i have tried two different reader pointer but its now giving exception.

需要一点帮助,谢谢

推荐答案

您可以按以下方式读取每个< InformationTuple> 中的第一个< Name> 元素.介绍以下扩展方法:

You can read the first <Name> element inside each <InformationTuple> as follows. Introduce the following extension methods:

public static partial class XmlReaderExtensions
{
    public static IEnumerable<string> ReadAllElementContentsAsString(this XmlReader reader, string localName, string namespaceURI)
    {
        while (reader.ReadToFollowing(localName, namespaceURI))
            yield return reader.ReadElementContentAsString();
    }

    public static IEnumerable<XmlReader> ReadAllSubtrees(this XmlReader reader, string localName, string namespaceURI)
    {
        while (reader.ReadToFollowing(localName, namespaceURI))
            using (var subReader = reader.ReadSubtree())
                yield return subReader;
    }
}

然后执行:

foreach (var name in reader.ReadAllSubtrees("InformationTuple", "")
    .Select(r => r.ReadAllElementContentsAsString("Name", "").First()))
{
    // Process the name somehow
    Debug.WriteLine(name);
}

如果您只想读取每个< InformationTuples> <>中每个< InformationTuple> 元素的第一个< Name> 元素,容器,您可以使用

If you want to only read the first <Name> element of each <InformationTuple> element inside each <InformationTuples> container, you can restrict the scope of the search by composing calls to ReadAllSubtrees() using SelectMany():

foreach (var name in reader.ReadAllSubtrees("InformationTuples", "")
    .SelectMany(r => r.ReadAllSubtrees("InformationTuple", ""))
    .Select(r => r.ReadAllElementContentsAsString("Name", "").First()))
{
    // Process the name somehow
    Debug.WriteLine(name);
}

一些注意事项:

在关闭新的阅读器之前,请勿对原始阅读器执行任何操作.不支持此操作,它可能导致无法预测的行为.

You should not perform any operations on the original reader until the new reader has been closed. This action is not supported and can result in unpredictable behavior.

因此,您必须在推进外部阅读器之前关闭或处置此嵌套阅读器.

Thus you must close or dispose this nested reader before advancing the outer reader.

开始时您对 reader.ReadToFollowing("InformationTuple"); 的调用过多.也许您打算做 reader.ReadToFollowing("InformationTuples"); ?

You have too many calls to reader.ReadToFollowing("InformationTuple"); at the beginning. Perhaps you meant to do reader.ReadToFollowing("InformationTuples");?

为确保每个< InformationTuple> 元素只有一个< Name> 元素,请替换 First() .Single().

To ensure there is one and only one <Name> element for each <InformationTuple> replace First() with .Single().

如果每个< InformationTuple> 的每个节点可能有多个< Name> 节点,并且您想读取所有这些节点,请执行以下操作:

If there might be multiple <Name> nodes for each <InformationTuple> and you want to read all of them, do:

foreach (var name in reader.ReadAllSubtrees("InformationTuple", "")
    .SelectMany(r => r.ReadAllElementContentsAsString("Name", "")))
{
    // Process the name somehow

演示小提琴此处.

这篇关于在C#中读取Mulitple子项并提取数据xmlReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆