1

As part of a project I need to parse some values from some XML which doesn't seem to be standard XML. The XML is stored in SQL Server. I need to query the database and retrieve this XML, then in C# I need to get the value of the XCoord and YCoord fields. Can someone show me how this could be achieved using System.Xml?

If anyone knows a SQL Query to return these values from the XML data, that would do just as well.

<AdapterItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.enceladus.com/Data">
  <Attributes>
    <Attribute>
      <Name>Process ID</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">1000</Value>
    </Attribute>
    <Attribute>
      <Name>Request</Name>
      <Value i:type="AdapterItem">
        <Attributes>
          <Attribute>
            <Name>Location</Name>
            <Value i:type="AdapterItem">
              <Attributes>
                <Attribute>
                  <Name>XCoord</Name>
                  <Value xmlns:d10p1="http://www.w3.org/2001/XMLSchema" i:type="d10p1:string">482557.53208923</Value>
                </Attribute>
                <Attribute>
                  <Name>YCoord</Name>
                  <Value xmlns:d10p1="http://www.w3.org/2001/XMLSchema" i:type="d10p1:string">240588.72462463</Value>
                </Attribute>
              </Attributes>
            </Value>
          </Attribute>
          <Attribute>
            <Name>Description</Name>
            <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Some Description</Value>
          </Attribute>
        </Attributes>
      </Value>
    </Attribute>
  </Attributes>
</AdapterItem>

Thanks in advance!

4
  • 2
    By "nonstandard", do you mean "poorly formed and does not parse"? Commented Jan 21, 2014 at 17:06
  • The XML is well formed but I was not familiar with its layout. You could say <Attribute name="XCoord">000.000</Attribute> but I realise that the value requires additional attributes of its own, such as 'type'. Commented Jan 22, 2014 at 2:03
  • Thanks for clarifying. I think it would be/would have been better to not say "nonstandard", because it is potentially misleading. But glad you got an answer, at any rate! Commented Jan 22, 2014 at 13:08
  • Sure thing. This was a misunderstanding. Commented Jan 22, 2014 at 16:08

3 Answers 3

1

Here is C# solution:

var doc = new XmlDocument();

doc.LoadXml(columnValueFromSql);

Console.WriteLine("XCoord={0}, YCoord={1}",
    doc.SelectSingleNode("//Attribute[Name='XCoord']/Value").InnerText,
    doc.SelectSingleNode("//Attribute[Name='YCoord']/Value").InnerText);

/* Outputs:

XCoord=482557.53208923, YCoord=240588.72462463

*/
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That works great. This works if I remove my namespace on line 1.
0

You could write some similar to this,

  WITH XMLNAMESPACES (http://www.w3.org/2001/XMLSchema' AS d4p1,
                      'http://schemas.enceladus.com/Data' AS message)

SELECT 
      CAST([XML_DATA] AS XML).value('(/AdapterItem/Attributes/Attribute[2]/Value/Attributes/Attribute[1]/Value/Attributes/Attribute[1]/Value)[1]', 'VARCHAR(20)') AS 'XCoord',
      CAST([XML_DATA] AS XML).value('(/AdapterItem/Attributes/Attribute[2]/Value/Attributes/Attribute[1]/Value/Attributes/Attribute[2])[1]', 'VARCHAR(20)') AS 'YCoord' FROM YOURTABLE

2 Comments

I think you're assuming things about the XML structure that may not always be true. You should probably be matching on the attribute names rather than their positions.
See top comment. It didn't seem right at first but the structure is designed like that.
0

A SQL Server query would look like this:

with xmlnamespaces(default 'http://schemas.enceladus.com/Data')
select T.XMLCol.value('(//Attribute[Name = "XCoord"]/Value/text())[1]', 'varchar(20)') as XCoord,
       T.XMLCol.value('(//Attribute[Name = "YCoord"]/Value/text())[1]', 'varchar(20)') as YCoord
from YourTable as T

1 Comment

Awesome. This works on the SQL side. I'd give you a vote up but haven't got sufficient reputation! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.