TSQL Abfrage mit XML Namespace

(C) 107er BLOG

Die Abfrage von einem XML mit Namespaces kann schon recht mühsam sein, zum Beispiel wenn man einen XML Wert von einem mit C# serialisiertem Objekt mit SQL abfragen möchte.

Wenn man ein in der Datenbank gespeichertes serialisiertes C# Objekt mit SQL durchsuchen möchte, muss man sich durch eine Menge Namespaces durch kämpfen. Aber es geht für manche Fälle auch einfacher.

Beispiel

Hier ein kleines Beispiel, wie ein Ausschnitt aus einem serialisiertem XML Objekt ausschauen kann. Das Ganze geht natürlich einfacher, und auch viel komplizierter, je nach Komplexität der Klasse in C#.

<ReadOnlyDictionaryInternalOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Runtime" z:Id="1" z:Type="System.Runtime.ReadOnlyDictionaryInternal`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" z:Assembly="System.ServiceModel.Internals, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
  <dictionary xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" z:Id="2" z:Type="System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" z:Assembly="0">
    <Version xmlns="" z:Id="3" z:Type="System.Int32" z:Assembly="0">2</Version>
    <Comparer xmlns="" z:Id="4" z:Type="System.Collections.Generic.GenericEqualityComparer`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" z:Assembly="0" />
    <HashSize xmlns="" z:Id="5" z:Type="System.Int32" z:Assembly="0">11</HashSize>
    <KeyValuePairs xmlns="" z:Id="6" z:Type="System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]][]" z:Assembly="0" z:Size="2">
      <KeyValuePairOfstringanyType xmlns="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
        <key z:Id="7">MyObject</key>
        <value z:Id="8" z:Type="Core.MyObject.Model.MyObject.Model" z:Assembly="Server.Core.MyObject.Model, Version=4.7.7.0, Culture=neutral, PublicKeyToken=null">
          <Number xmlns="http://Core.MyObject.Model" z:Id="9" />
          <Date xmlns="http://Core.MyObject.Model" i:nil="true" />
          <Period xmlns="http://Core.MyObject.Model" z:Id="10">
            <DateFrom>2013-10-01T00:00:00</DateFrom>
            <DateTo>2013-10-31T00:00:00</DateTo>
            <Name z:Id="11">2014/9 (2013-10-01 - 2013-10-31)</Name>
          </Period>
          <Text xmlns="http://Core.MyObject.Model" i:nil="true" />
          <ArchiveID xmlns="http://Core.MyObject.Model" z:Id="13">MyArchive</ArchiveID>
          <BIC xmlns="http://Core.MyObject.Model" z:Id="15">XXXXXX</BIC>
          <Bankcode xmlns="http://Core.MyObject.Model" z:Id="16">1234567890</Bankcode>
          <BarcodeID xmlns="http://Core.MyObject.Model" z:Id="17">56895</BarcodeID>
          <Currency xmlns="http://Core.MyObject.Model" i:nil="true" />
          <CurrencyID xmlns="http://Core.MyObject.Model" z:Id="22">EUR</CurrencyID>
          <DataVC1 xmlns="http://Core.MyObject.Model" z:Id="23">I</DataVC1>
          <DateOfReceipt xmlns="http://Core.MyObject.Model">2013-10-04T13:57:47.0043325Z</DateOfReceipt>
          <DocumentID xmlns="http://Core.MyObject.Model" z:Id="24">ABC12345.doc</DocumentID>
          <DueDate xmlns="http://Core.MyObject.Model">2013-10-24T13:57:47.0043325Z</DueDate>
          <ExtNumber xmlns="http://Core.MyObject.Model" z:Id="25">FA49926745214</ExtNumber>
          <RecordList xmlns="http://Core.MyObject.Model" z:Id="26">
            <items z:Id="27" z:Type="System.Collections.Generic.List`1[[Core.MyObject.Model.MyObject.AccountingRecord, Server.Core.MyObject.Model, Version=4.7.7.0, Culture=neutral, PublicKeyToken=null]]" z:Assembly="0">

Abfrage

Diese Abfrage sollte den Wert „MyObject“ zurück liefern.

SELECT
  Id,
  SerializedValue.value('(/*:ReadOnlyDictionaryInternalOfstringanyType/*:dictionary/*:KeyValuePairs/*:KeyValuePairOfstringanyType/*:key)[1]','varchar(MAX)') AS KeyName
FROM MyTable

 

Einen Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht.


*


65 − 62 =