Reputation: 3659
I need to get a specific element from an xml string
to know its corrensponding concrete type
to deserialize
it. Let's call Function Code
as the specific element and getting this element is a little challenging for me.
Each function code
's corresponds to a specific schema design, and it looks like this:
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
. But notice that on line 3
that the element name is specific to its concrete type
also.
So for another Function Code, e.g. FunctionCode2
, the element on line 3
will not be the same:
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. Is there a better approach for this without reading the whole string?
Here is a sample XML
I got:
<?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>
Upvotes: 0
Views: 41
Reputation: 117084
So given you have two XDocument
, for each sample XML, called doc1
and doc2
respectively, then this code:
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:
var ns = doc.Root.GetDefaultNamespace();
var functionCode = doc.Root.Descendants(ns + "FunctionCode").First().Value;
Upvotes: 1