代码之家  ›  专栏  ›  技术社区  ›  KaeL

获取不同xmls上的特定元素

  •  0
  • KaeL  · 技术社区  · 8 年前

    我需要从 xml string 知道它的对应关系 concrete type deserialize 它。让我们打电话 Function Code 作为一个特定的元素,得到这个元素对我来说有点挑战。

    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>
    

    我需要上的函数代码元素的值 line 5 哪个是 FunctionCode1 . 但请注意 line 3 元素名特定于 混凝土类型 也.

    所以对于另一个函数代码,例如。 FunctionCode2 ,上的元素 第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>
    

    我只想用 string.IndexOf("<FunctionCode>") 得到 功能代码 的值,直到找到相应的结束标记。有没有更好的方法来解决这个问题而不去阅读整个字符串?

    这是一个样品 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>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Enigmativity    8 年前

    既然你有两个 XDocument ,对于每个示例xml,调用 doc1 doc2 分别是,那么这个代码:

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

    …生产:

    FunctionCode1
    FunctionCode2
    

    因此,如果您有一个这种格式的未知xml文档,一般情况是:

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