1

Which is better way to load SqlXml into XmlDocument (if any) without intermediate string?

I need to validate xml using xsd from database inside CLR UDF. I try to optimize my code.

My code is:

        schema1 = XmlSchema.Read(new StringReader(cXmlSchemaText), ValidateSchema);

        asset1.Schemas.Add(schema1);
        asset1.LoadXml(ValueString.ToString());
        asset1.Validate(ValidateXmlValue);

where ValueString is my SqlXml.

5
  • Just curious: why post this to S.O. when the 2 related questions are on DBA.SE? I would think this should also be on DBA.SE, especially with it being SQLCLR which only exists within the context of SQL Server. Or is this a better site for the C# aspect of the question? Commented Jan 13, 2016 at 14:52
  • You are right, but my problem is mainly C# related I decided that here I get more answers. If you can tranfer it to DBA.SE, @srutzky. Commented Jan 13, 2016 at 14:58
  • I don't know how to migrate, or even that it necessarily should. I was just wondering as there are many more SQLCLR questions here on S.O. than on DBA.SE, yet in my mind, that is something that should be on DBA.SE, but I could be wrong about that. It might just be my preference. Commented Jan 13, 2016 at 15:00
  • I ususally try to ask question connected with SQL in DBA.SE. I also wonder where to put this question (and you noted that I already have 2 more connected with this inside DBA.SE). I think here are more persons with experience in C# and will provide beter solution. Commented Jan 13, 2016 at 15:09
  • Ok. Again, I was just curious :-) Commented Jan 13, 2016 at 15:18

1 Answer 1

4

First, even with .ToString() and .Value returning the same string value, it is probably best to use ValueString.Value for the sake of consistency in terms of accessing values within the Sql* types.

You should be able to skip the intermediate string by using the SqlXml.CreateReader method which should, according to the documentation, be returning the internal XmlReader of the SqlXml variable. Your code would change very slightly to be:

asset1.Load(ValueString.CreateReader());
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.