2

This is my column value in sql:

<ProductId>101</ProductId>
<ProductName>TShirt</ProductName>
<Description>UCB</Description>
<Category>199</Category>
<Weight>150</Weight>
<Price>2500</Price>
<images></images>

Sometimes it looks like this also:

<ProductId xsi:nil="true" ></ProductId> 
<ProductName>Toys</ProductName>
<Description xsi:nil="true" ></Description>
<Category xsi:nil="true" ></Category>
<Weight xsi:nil="true" ></Weight>
<Price xsi:nil="true" ></Price>
<images></images>

I want to extract 'ProductName' value i.e. TShirt from this column. It could be any node, How do I do this?

This is my c# code

var xml = XElement.Parse(xmlString);

string fieldValue = xml.Element(fieldToSearch).ToString();

But since it is not proper xml string it gives error so I wrote explicitly

string xmlString = "<root>" + columnValue + "</root>";

What is the correct way to find the node value?

Edit: The value can be extrtacted for Weight/Description/Price, any node. The error comes when xsi:nil is found.

5
  • 1
    Any way you can change how data gets stored in SQL? what you did might be a-okay but storing invalid xml is a bit meh imo Commented Mar 8, 2019 at 12:21
  • I would wrap the xml in a root node similar to how you are doing it, maybe not by string concatenation, but that should work. What is the problem? Commented Mar 8, 2019 at 12:22
  • The error is due to innertext being null. Try following : string fieldValue = (string)xml.Element(fieldToSearch; ToString() will fail when null. Commented Mar 8, 2019 at 12:24
  • It could be any node that means your TShirt value present on any column? or in <ProductName> Commented Mar 8, 2019 at 12:25
  • @jdweng ToString() will fail if the element is null, which will only happen if it's missing. If the value is null or empty, there's no problem. Commented Mar 9, 2019 at 9:00

1 Answer 1

2

You're getting an error because the xsi:nil attribute has a namespace prefix of xsi. As you're missing the parent element, there's no declaration of this prefix.

Ideally, you'd fix the source of this so that you store well formed XML rather than a bunch of child elements, but if you're stuck with it then the easiest way to get around the issue is to add the namespace declaration to your dummy root element:

var xmlString = 
    "<root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + 
    columnValue + 
    "</root>";

Secondly, I'd note that ToString() will return the element itself, e.g. <ProductName>Toys</ProductName>. If you just want the value, then you can use the explicit conversion:

var fieldValue = (string) xml.Element(fieldToSearch);

See this fiddle for a working demo.

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

Comments

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.