Skip to content

Instantly share code, notes, and snippets.

@bitops
Created August 3, 2017 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitops/412a1e4d8d3778fcbeaf6d681f3f8e0a to your computer and use it in GitHub Desktop.
Save bitops/412a1e4d8d3778fcbeaf6d681f3f8e0a to your computer and use it in GitHub Desktop.
Simple XmlDocument example with Couchbase on .Net
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