How to CData property? - c#

How can I serialize a property with CData? I have tried a few different methods including making the original property XmlIgnore and introducing a property which returns XmlCDataSection. None have worked so far.
I have the following runnable console test which shows the error. How can I modify this to allow the Regex data to serialize and deserialize.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
[Serializable]
public class MyRegex
{
public string Regex { get; set; }
}
public static class SerializerHelper<T>
{
public static string Serialize(T myobject)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, myobject);
string xml = stringWriter.ToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}
public static T DeSerialize(string xml)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader stringReader = new StringReader(xml);
return (T)xmlSerializer.Deserialize(stringReader);
}
}
class Program
{
static void Main(string[] args)
{
MyRegex original = new MyRegex { Regex = "\b[1-3]{1}\b#Must be a value of 1 to 3" };
string xml = SerializerHelper<MyRegex>.Serialize(original);
Console.WriteLine("--- SERIALIZE ---");
Console.WriteLine(xml);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("--- DESERIALIZE ---");
MyRegex deSerial = SerializerHelper<MyRegex>.DeSerialize(xml);
Console.WriteLine("Equals? " + (deSerial.Regex.Equals(original.Regex)));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Console.ReadKey();");
Console.ReadKey();
}
}
}
Additional: Attempted replace method - not working
private string _regex;
public string Regex
{
get { return _regex.Replace(#"\\", #"\").Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("&apos;", "'"); }
set { _regex = value.Replace(#"\", #"\\").Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "&apos;"); }
}

You don't need to use CData here - the problem is that your Regex does not have the string "\b", it does have the \u0008 (BS) character - which is not what you need in the regular expression. If you escape the '\' in the MyRegex initialization, it should work:
MyRegex original = new MyRegex { Regex = "\\b[1-3]{1}\\b#Must be a value of 1 to 3" };
This console app is ready to run, and it serializes the data fine (using \b):
public class StackOverflow_6755014
{
public class MyRegex
{
public string Regex { get; set; }
}
public static class SerializerHelper<T>
{
public static string Serialize(T myobject)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, myobject);
string xml = stringWriter.ToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}
public static T DeSerialize(string xml)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader stringReader = new StringReader(xml);
return (T)xmlSerializer.Deserialize(stringReader);
}
}
public static void Test()
{
MyRegex original = new MyRegex { Regex = "\\b[1-3]{1}\\b#Must be a value of 1 to 3" };
string xml = SerializerHelper<MyRegex>.Serialize(original);
Console.WriteLine("--- SERIALIZE ---");
Console.WriteLine(xml);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("--- DESERIALIZE ---");
MyRegex deSerial = SerializerHelper<MyRegex>.DeSerialize(xml);
Console.WriteLine("Equals? " + (deSerial.Regex.Equals(original.Regex)));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Console.ReadKey();");
Console.ReadKey();
}
}

Related

how to save/load data to n xml file

i'm trying to load and save my data in my datagrid to an xml file using Singleton Design.
i have created
public class DataProvider
{
private static DataProvider singletonInstance = new DataProvider();
private ObservablePerson Person;
/// <summary>
/// Private constructor
/// </summary>
private DataProvider()
{
}
public static DataProvider GetInstance()
{
if (singletonInstance == null)
singletonInstance = new DataProvider();
return singletonInstance;
}
public bool SaveToXml(List<Person> PersonsList)
{
return false;
}
public List<Person> LoadFromX()
{
return new List<Person>() { new Person() { name= "jhon" } };
}
}
}
and this is the object i want to save
[Serializable]
public class ObservablePerson : ObservableObject
{
private string _name;
public string name
{
get
{
return _name;
}
set
{
_name= value;
NotifyPropertyChanged();}
and i also created a view model form from person.
what should i do to save those data in my datagrid in a xml file .
thanks.
Read an XML file using XmlTextReader and call Read method to read its node one by one until the end of file.
using System;
using System.Xml;
namespace ReadXml1 {
class Class1 {
static void Main(string[] args) {
// Create an isntance of XmlTextReader and call Read method to read the file
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
textReader.Read();
// If the node has value
while (textReader.Read()) {
// Move to fist element
textReader.MoveToElement();
Console.WriteLine("XmlTextReader Properties Test");
Console.WriteLine("===================");
// Read this element's properties and display them on console
Console.WriteLine("Name:" + textReader.Name);
Console.WriteLine("Base URI:" + textReader.BaseURI);
Console.WriteLine("Local Name:" + textReader.LocalName);
Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString());
Console.WriteLine("Depth:" + textReader.Depth.ToString());
Console.WriteLine("Line Number:" + textReader.LineNumber.ToString());
Console.WriteLine("Node Type:" + textReader.NodeType.ToString());
Console.WriteLine("Attribute Count:" + textReader.Value.ToString());
}
}
}
}
and for creating and save to XML file:
using System;
using System.Xml;
namespace ReadingXML2 {
class Class1 {
static void Main(string[] args) {
// Create a new file in C:\\ dir
XmlTextWriter textWriter = new XmlTextWriter("C:\\myXmFile.xml", null);
// Opens the document
textWriter.WriteStartDocument();
// Write comments
textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
textWriter.WriteComment("myXmlFile.xml in root dir");
// Write first element
textWriter.WriteStartElement("Student");
textWriter.WriteStartElement("r", "RECORD", "urn:record");
// Write next element
textWriter.WriteStartElement("Name", "");
textWriter.WriteString("Student");
textWriter.WriteEndElement();
// Write one more element
textWriter.WriteStartElement("Address", "");
textWriter.WriteString("Colony");
textWriter.WriteEndElement();
// WriteChars
char[] ch = new char[3];
ch[0] = 'a';
ch[1] = 'r';
ch[2] = 'c';
textWriter.WriteStartElement("Char");
textWriter.WriteChars(ch, 0, ch.Length);
textWriter.WriteEndElement();
// Ends the document.
textWriter.WriteEndDocument();
// close writer
textWriter.Close();
}
}
}
Let's use Xml and Xml.Serialization:
using System.Xml;
using System.Xml.Serialization;
On your singleton, implements
public static List<T> LoadFromXml<T>(string path, string fileName)
{
var xmlSerializer = new XmlSerializer(typeof(List<T>));
var xmlText = File.ReadAllText(Path.Combine(path, fileName));
return (List<T>)xmlSerializer.Deserialize(new StringReader(xmlText));
}
public static bool SaveToXml<T>(List<T> list, string path, string fileName)
{
var xmlText = Serialize<T>(list);
try
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
File.WriteAllText(Path.Combine(path, fileName), xmlText);
return true;
}
catch(Exception e)
{
return false;
}
}
private static string Serialize<T>(List<T> list)
{
if (list == null || !list.Any())
return string.Empty;
var xmlSerializer = new XmlSerializer(typeof(List<T>));
using (var stringWriter = new StringWriter())
{
using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true }))
{
xmlSerializer.Serialize(xmlWriter, list);
return stringWriter.ToString();
}
}
}
Using generic T, we can use the same methods to save other types of records
To call:
var person1 = new Person()
{
name = "jhon",
};
var person2 = new Person()
{
name = "another jhon",
};
var personList = new List<Person>();
personList.Add(person1);
personList.Add(person2);
var pathXml = #"C:\testes\xml";
var fileName = "persons.xml";
var dataProvider = DataProvider.GetInstance();
var itsSaved = dataProvider.SaveToXml<Person>(personList, pathXml, fileName);
var persons = dataProvider.LoadFromXml<Person>(pathXml, fileName);

C# Serialize object to SOAP string array issue

I have an issue where trying to serialize an object containing a string array to soap causes an exception in my application. I am doing the following to create the soap formatter:
XmlTypeMapping mapping = new SoapReflectionImporter().ImportTypeMapping(obj.GetType());
XmlSerializer serializer = new XmlSerializer(mapping);
when I call Serialize on the serializer I get the following exception. "Token StartElement in state Epilog would result in an invalid XML document."
However if I just want regular xml and create my XmlSerializer like this:
XmlSerializer serializer = new XmlSerializer(obj.GetType());
Everything works fine and the xml contains the string array.
I have a full example below that reproduces the issue on my machine if someone could take a look I would be very grateful as I am out of ideas!
static void Main(string[] args)
{
GetAlarmEventTypesResponse bob = new GetAlarmEventTypesResponse();
bob.GetAlarmEventTypesTypes = new string[] { "bob", "bob1", "bob2" };
bob.version = "2.0";
// works
string xml = GetRegularDocument(bob);
Console.WriteLine(xml);
// throws exception
string soap = GetSoapDocument(bob);
Console.WriteLine(soap);
}
//------------------------------------------------------------------------------
[System.Xml.Serialization.SoapTypeAttribute(Namespace = "http://example/common/dataexchange/2011/05")]
public class GetAlarmEventTypesResponse
{
public GetAlarmEventTypesResponse()
{
version = "1.2";
}
[System.Xml.Serialization.XmlArrayItemAttribute("Type", IsNullable = false)]
public string[] GetAlarmEventTypesTypes { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string version { get; set; }
}
//------------------------------------------------------------------------------
public static string GetRegularDocument(object obj)
{
string document = null;
XmlSerializer serializer = new XmlSerializer(obj.GetType());
using (StringWriter textWriter = new StringWriter())
{
serializer.Serialize(textWriter, obj);
document = textWriter.ToString();
}
return document;
}
//------------------------------------------------------------------------------
public static string GetSoapDocument(object obj)
{
string document = null;
XmlTypeMapping mapping = new SoapReflectionImporter().ImportTypeMapping(obj.GetType());
XmlSerializer serializer = new XmlSerializer(mapping);
using (StringWriter textWriter = new StringWriter())
{
serializer.Serialize(textWriter, obj);
document = textWriter.ToString();
}
return document;
}

XML serialisation namespace on the wrong element

I'm posting XML to an API which has been serialised from a C# class at my end, everything looks correct apart from where a particular namespace is when the XML is generated but I am not sure why.
I need the a namespace to be be on the appointmentAvailability element. Ignore the escape backslashes that's just because I copied the xml out of the visual studio debugger.
Where am I going wrong?
function to test
var a = new AppointmentAvailability
{
TimeSlot = "AM"
};
var aa = new AppointmentAvailabilityContainer();
aa.appointmentAvailability = a;
var nd = new Dictionary<string, string>();
nd.Add("a", "http://maindomain/Appointments");
var x = XmlManager.SerializeThis<AppointmentAvailabilityContainer>(aa,nd);
var checkappointmentavailability = FluidWeb.CheckAppointmentAvailability(a);
generic serialise function
public static string SerializeThis<T>(object obj, Dictionary<string, string> dictionary)
{
try
{
XmlDocument xd = new XmlDocument();
string xml = "";
var xs = new XmlSerializer(obj.GetType());
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
foreach (var item in dictionary)
{
xsn.Add(item.Key, item.Value);
}
using (MemoryStream ms = new MemoryStream())
{
xs.Serialize(ms, obj, xsn);
ms.Position = 0;
xd.Load(ms);
xml = xd.InnerXml;
}
return xml;
}
catch(XmlException x)
{
var xml = "Could not serialise.";
return xml;
}
}
classes
[XmlRoot("AppointmentAvailability", Namespace = "http://maindomain")]
public class AppointmentAvailabilityContainer
{
[XmlElement("appointmentAvailability")]
public AppointmentAvailability appointmentAvailability { get; set; }
}
[XmlRoot("appointmentAvailability", Namespace = "http://maindomain/Appointments")]
[XmlType("a")]
public class AppointmentAvailability
{
[XmlElement("TimeSlot")]
public string TimeSlot { get; set; }
}
xml generated
<?xml version=\"1.0\"?>
<AppointmentAvailability xmlns:a=\"http://maindomain/Appointments\" xmlns=\"http://maindomain">
<appointmentAvailability>
<a:TimeSlot>AM</a:TimeSlot>
</appointmentAvailability>
</AppointmentAvailability>
xml trying to achieve
<?xml version=\"1.0\"?>
<AppointmentAvailability xmlns=\"http://maindomain">
<appointmentAvailability xmlns:a=\"http://maindomain/Appointments\">
<a:TimeSlot>AM</a:TimeSlot>
</appointmentAvailability>
</AppointmentAvailability>

XmlWriter to both String and File

Can I use an XmlWriter to write to both a file and a string?
When writing to a file I do this:
using (var xw = XmlWriter.Create("myFile.xml"))
{
//Build the xml
}
And when writing to a string I do this:
using (var sw = new StringWriter())
{
using (var xw = XmlWriter.Create(sw))
{
// Build the xml
}
return sw.ToString();
}
But can I write to both a file and a string with the same XmlWriter instance?
You could create a method like this:
private static void WriteXML(TextWriter writer)
{
using (var xw = XmlWriter.Create(writer))
{
// Build the xml
}
}
And then call it like:
using (StreamWriter sw = new StreamWriter(...))
WriteXML(sw);
to write a file or
string xml = String.Empty;
using (StringWriter sw = new StringWriter())
{
WriteXML(sw);
xml = sw.ToString();
}
To create the string.
You could even create helper methods:
private static void WriteXMLFile(string fileName)
{
using (StreamWriter sw = new StreamWriter(...))
WriteXML(sw);
}
private static string GetXML()
{
using (StringWriter sw = new StringWriter())
{
WriteXML(sw);
return sw.ToString();
}
}
Create a composite XmlWriter which wraps many XmlWriter instances.
public class CompositeXmlWriter : XmlWriter
{
private readonly IEnumerable<XmlWriter> writers;
public CompositeXmlWriter(IEnumerable<XmlWriter> writers)
{
this.writers = writers;
}
public override void WriteStartDocument()
{
foreach (var writer in writers)
{
writer.WriteStartDocument();
}
}
public override void WriteEndDocument()
{
foreach (var writer in writers)
{
writer.WriteEndDocument();
}
}
...Implement all other methods
}
Create your CompositeXmlWriter with other XmlWriters.
using (var xw = new CompositeXmlWriter(new[] { XmlWriter.Create("myFile.xml"), XmlWriter.Create(new StringWriter())})
{
//Build the xml
}
Now whatever written in xw will be written in all the XmlWriters.

Can I Serialize XML straight to a string instead of a Stream with C#?

This example uses a StringWriter to hold the serialized data, then calling ToString() gives the actual string value:
Person john = new Person();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, john);
string serializedXML = stringWriter.ToString();
Is there any easier/Cleaner way to do this? All of the Serialize() overloads seem to use a Stream or Writer.
UPDATE: Asked a similar question about serializing an IEnumerable via an Extension Method .
Fun with extension methods...
var ret = john.ToXmlString()
public static class XmlTools
{
public static string ToXmlString<T>(this T input)
{
using (var writer = new StringWriter())
{
input.ToXml(writer);
return writer.ToString();
}
}
public static void ToXml<T>(this T objectToSerialize, Stream stream)
{
new XmlSerializer(typeof(T)).Serialize(stream, objectToSerialize);
}
public static void ToXml<T>(this T objectToSerialize, StringWriter writer)
{
new XmlSerializer(typeof(T)).Serialize(writer, objectToSerialize);
}
}
More or less your same solution, just using an extension method:
static class XmlExtensions {
// serialize an object to an XML string
public static string ToXml(this object obj) {
// remove the default namespaces
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
// serialize to string
XmlSerializer xs = new XmlSerializer(obj.GetType());
StringWriter sw = new StringWriter();
xs.Serialize(sw, obj, ns);
return sw.GetStringBuilder().ToString();
}
}
[XmlType("Element")]
public class Element {
[XmlAttribute("name")]
public string name;
}
class Program {
static void Main(string[] args) {
Element el = new Element();
el.name = "test";
Console.WriteLine(el.ToXml());
}
}
I created this helper method, but I haven't tested it yet. Updated the code per orsogufo's comments (twice):
private string ConvertObjectToXml(object objectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(objectToSerialize.GetType());
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, objectToSerialize);
return stringWriter.ToString();
}
Seems like no body actually answered his question, which is no, there is no way to generate an XML string without using a stream or writer object.

Categories