【问题标题】:Extract full XML tree out of part of ID从部分 ID 中提取完整的 XML 树
【发布时间】:2015-05-07 08:00:00
【问题描述】:

应该很容易,但我不知道怎么做:

我有这样的身份证号码:

ID1000

我想得到这个:

                <xf:instance id="etykiety_ID1000" xmlns="">
                    <sek>
                        <etykiety_ref>
                            <opt>
                                <item>dokument wymagany</item>
                                <value>1</value>
                            </opt>
                            <opt>
                                <item>dokument niewymagany</item>
                                <value>2</value>
                            </opt>
                            <opt>
                                <item>potwierdzenie sprawdzenia</item>
                                <value>3</value>
                            </opt>
                        </etykiety_ref>
                    </sek>
                </xf:instance>

这也是:

<xf:bind id="ID1000"
    nodeset="wnio:TrescDokumentu/wnio:Wartosc/wnio:ID1000" relevant="true()"/>

不是这个:

                <xf:instance id="etykiety_ID56" xmlns="">
                    <sek>
                        <etykiety_ref>
                            <opt>
                                <item>dokument wymagany</item>
                                <value>1</value>
                            </opt>
                            <opt>
                                <item>dokument niewymagany</item>
                                <value>2</value>
                            </opt>
                            <opt>
                                <item>potwierdzenie sprawdzenia</item>
                                <value>3</value>
                            </opt>
                        </etykiety_ref>
                    </sek>
                </xf:instance>

换句话说,我想获取所有 ID1000 的出现以及里面的所有 artibutes 和东西。

对不起,如果我的问题是基础知识,我是初学者。

【问题讨论】:

标签: c# xml


【解决方案1】:

使用 XML Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            var results = doc.Root.Elements()
                .Where(x => x.Attribute("id").Value.EndsWith("ID1000"));
        }
    }
}​

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多