Created
August 3, 2017 18:38
-
-
Save bitops/412a1e4d8d3778fcbeaf6d681f3f8e0a to your computer and use it in GitHub Desktop.
Simple XmlDocument example with Couchbase on .Net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Threading; | |
using System.Xml; | |
using Couchbase; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Cluster cluster = new Cluster(); | |
XmlDocument xmlDoc = new XmlDocument(); | |
XmlNode rootNode = xmlDoc.CreateElement("users"); | |
xmlDoc.AppendChild(rootNode); | |
XmlNode userNode = xmlDoc.CreateElement("user"); | |
XmlAttribute attribute = xmlDoc.CreateAttribute("age"); | |
attribute.Value = "42"; | |
userNode.Attributes.Append(attribute); | |
userNode.InnerText = "Harish"; | |
rootNode.AppendChild(userNode); | |
using (var stringWriter = new StringWriter()) | |
{ | |
using (var xmlTextWriter = XmlWriter.Create(stringWriter)) | |
{ | |
xmlDoc.WriteTo(xmlTextWriter); | |
xmlTextWriter.Flush(); | |
var xmlString = stringWriter.GetStringBuilder().ToString(); | |
Console.WriteLine("XML Document that we put in:"); | |
Console.WriteLine(xmlString); | |
using (var bucket = cluster.OpenBucket()) | |
{ | |
var document = new Document<XmlDocument>(); | |
document.Id = "FooBarBaz"; | |
document.Content = xmlDoc; | |
var upsert = bucket.Upsert(document); | |
if (!upsert.Success) | |
{ | |
Console.WriteLine("failed to insert document"); | |
Environment.Exit(1); | |
} | |
} | |
} | |
} | |
using (var bucket = cluster.OpenBucket()) | |
{ | |
var document = bucket.GetDocument<XmlDocument>("FooBarBaz"); | |
var xmlDocRead = document.Content; | |
using (var stringWriter = new StringWriter()) | |
{ | |
using (var xmlTextWriter = XmlWriter.Create(stringWriter)) | |
{ | |
xmlDocRead.WriteTo(xmlTextWriter); | |
xmlTextWriter.Flush(); | |
var xmlString = stringWriter.GetStringBuilder().ToString(); | |
Console.WriteLine("XML Document that we read back out:"); | |
Console.WriteLine(xmlString); | |
} | |
} | |
} | |
Thread.Sleep(2000); | |
Environment.Exit(0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment