开发者

How to transform from SQL Server text field to xml and back

We save our xml in a SQL Server database with the data type text, so it's a long string.

What I now need to do is transform this string to xml, and then work with this and finally transform it back to a string to be saved back to the database.

The xml:

<Posts>
    <DialogPost>
      <Type></开发者_Python百科Type>
      <User></User>
      <Customer></Customer>
      <Date></Date>
      <Message></Message>
    </DialogPost>
</Posts>

I've started working with Linq to xml, and it seems to be great but I'm having problem making it work.

XDocument dialogXML = XDocument.Load("trying to load the xml string right in");

It doesn't like the string straight from the db. How would you recommend me to tackle this problem? Both reading and writing back to the db.


Use the XDocument.Parse method. Load expects a URI not XML.

You should also note that the XML you posted is not valid. You have a closing Dialog tag at the end but no opening tag. Here is an example that would work with valid XML.

string str =
@"<?xml version=""1.0""?>
<!-- comment at the root level -->
<Posts>
    <DialogPost>
      <Type></Type>
      <User></User>
      <Customer></Customer>
      <Date></Date>
      <Message></Message>
    </DialogPost>
  </Posts>";
XDocument doc = XDocument.Parse(str);

Take a look here for an example and more info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜