简体   繁体   English

获取不同XML上的特定元素

[英]Get Specific Element on Different XMLs

I need to get a specific element from an xml string to know its corrensponding concrete type to deserialize it. 我需要从xml string获取特定元素,以了解其对应的concrete typedeserialize它。 Let's call Function Code as the specific element and getting this element is a little challenging for me. 让我们将Function Code称为特定元素,获取此元素对我来说有点挑战。

Each function code 's corresponds to a specific schema design, and it looks like this: 每个function code对应一个特定的架构设计,如下所示:

1    <?xml version="1.0" encoding="utf-8"?>
2    <Document xmlns="some.namespace.of.schema.design.1">
3      <SchemaDesign1>
4        <Header>
5          <FunctionCode>FunctionCode1</FunctionCode>
6          <OtherElement1>...</OtherElement1>
7          <OtherElement2>...</OtherElement2>

I need the value of the Function Code element on line 5 which is FunctionCode1 . 我需要第line 5上的Function Code元素的值是FunctionCode1 But notice that on line 3 that the element name is specific to its concrete type also. 但是请注意,在line 3上,元素名称也特定于其concrete type

So for another Function Code, eg FunctionCode2 , the element on line 3 will not be the same: 因此,对于另一个功能代码,例如FunctionCode2 ,第line 3上的元素将不同:

1    <?xml version="1.0" encoding="utf-8"?>
2    <Document xmlns="some.namespace.of.schema.design.2">
3      <SchemaDesign2>
4        <Header>
5          <FunctionCode>FunctionCode2</FunctionCode>
6          <OtherElement1>...</OtherElement1>
7          <OtherElement2>...</OtherElement2>

I can only think of using string.IndexOf("<FunctionCode>") and get the function code 's value until it finds the respective closing tag. 我只能想到使用string.IndexOf("<FunctionCode>")并获取function code的值,直到找到相应的结束标记为止。 Is there a better approach for this without reading the whole string? 是否有一个更好的方法而不读取整个字符串?

Here is a sample XML I got: 这是我得到的示例XML

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:caaa.013.001.07">
    <AccptrDgnstcReq>
        <Hdr>
            <MsgFctn>DGNP</MsgFctn>
            <PrtcolVrsn>6.0</PrtcolVrsn>
            <XchgId>378</XchgId>
            <CreDtTm>2013-08-08T22:18:35.07+02:00</CreDtTm>
            <InitgPty>
                <Id>66000001</Id>
                <Tp>OPOI</Tp>
                <Issr>ACQR</Issr>
            </InitgPty>
            <RcptPty>
                <Id>epas-acquirer-1</Id>
                <Tp>ACQR</Tp>
            </RcptPty>
        </Hdr>
        <DgnstcReq>
            <Envt>
                <Acqrr>
                    <Id>
                    <Id>9287351</Id>
                    </Id>
                    <ParamsVrsn>2013-08-07 08:00:00</ParamsVrsn>
                </Acqrr>
                <MrchntId>
                    <Id>EPASMER001</Id>
                </MrchntId>
                <POIId>
                    <Id>66000001</Id>
                    <Tp>OPOI</Tp>
                    <Issr>ACQR</Issr>
                </POIId>
            </Envt>
        </DgnstcReq>
        <SctyTrlr>
            <CnttTp>AUTH</CnttTp>
            <AuthntcdData>
                <Rcpt>
                    <KEK>
                        <KEKId>
                            <KeyId>SpecV1TestKey</KeyId>
                            <KeyVrsn>2010060715</KeyVrsn>
                            <DerivtnId>OYclpQE=</DerivtnId>
                        </KEKId>
                        <KeyNcrptnAlgo>
                            <Algo>DKP9</Algo>
                        </KeyNcrptnAlgo>
                        <NcrptdKey>4pAgABc=</NcrptdKey>
                    </KEK>
                </Rcpt>
                <MACAlgo>
                    <Algo>MCCS</Algo>
                </MACAlgo>
                <NcpsltdCntt>
                    <CnttTp>DATA</CnttTp>
                </NcpsltdCntt>
                <MAC>3Dahc1K96Pc=</MAC>
            </AuthntcdData>
        </SctyTrlr>
    </AccptrDgnstcReq>
</Document>

So given you have two XDocument , for each sample XML, called doc1 and doc2 respectively, then this code: 因此,考虑你有两个XDocument ,对每个样品XML,叫doc1doc2分别,那么这段代码:

var ns1 = doc1.Root.GetDefaultNamespace();
var ns2 = doc2.Root.GetDefaultNamespace();

var functionCode1 = doc1.Root.Descendants(ns1 + "FunctionCode").First().Value;
var functionCode2 = doc2.Root.Descendants(ns2 + "FunctionCode").First().Value;

Console.WriteLine(functionCode1);
Console.WriteLine(functionCode2);

...produces: ...生产:

FunctionCode1
FunctionCode2

So, the general case for this, given you have an unknown XML document in this format, is: 因此,考虑到您有这种格式的未知XML文档,通常的情况是:

var ns = doc.Root.GetDefaultNamespace();

var functionCode = doc.Root.Descendants(ns + "FunctionCode").First().Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM