I've got a WPF C# program and at one point I need to serialize objects to XML. In other places, I've been using this:
TextWriter writer = new StreamWriter(xmlFilePath);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MYOBJECT_TYPE));
try
{
xmlSerializer.Serialize(writer, MYOBJECT);
}
catch (Exception ex)
{
MessageBox.Show("Exception occured while writing to Xml" + ex.Message);
}
finally
{
writer.Close();
}
This is fantastic, but this means I have to have a different XML file for every object I want to serialize. How do I use this method (with the least amount of modifications) to serialize the object to the XML WITHIN a parent element? That way, when I want to deserialize the object later, I can just find the element that I want, and deserialize everything within that element.
As requested, here is CreateDefaultXml();:
static void CreateDefaultXml()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<StoredObjects></StoredObjects>");
XmlNode root = doc.DocumentElement;
try
{
doc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine("Exception occured while creating Xml" + ex.InnerException);
}
}
EDIT:
Currently, this is what I've got (but it throws an exception There was an error generating the XML document.)
if (!File.Exists(xmlFilePath))
CreateDefaultXml();
XDocument doc = XDocument.Load(xmlFilePath);
var element = doc.Descendants("Object").Where(x => x.Attribute("Name").Value.Equals("objectName")).SingleOrDefault();
if (element == null)
{
element = new XElement("Object", new XAttribute("Name", objectName));
doc.Element("StoredObjects").Add(element);
}
XmlWriter writer = element.CreateWriter();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MYOBJECT_TYPE));
try
{
xmlSerializer.Serialize(writer, MYOBJECT);
}
catch (Exception ex)
{
MessageBox.Show("Exception occured while writing to Xml: " + ex.Message);
}
finally
{
writer.Close();
doc.Save(xmlFilePath);
}
You are trying to serialize directly to some nested XElement inside an XDocument using XmlSerializer. Unfortunately, it seems that, when serializing directly to a LINQ-to-XML XElement using XmlSerializer together with XContainer.CreateWriter(), it is actually necessary to serialize to an empty XDocument thereby creating its root element. (I am not sure why this restriction exists, but it does.) Since this doesn't meet your needs, it is easy to serialize to a temporary XDocument, remove its root node, then add that node to your overall element hierarchy.
Also, when adding an object to your database using an objectName that already has XML data stored, you need to remove the old data.
I refactored your code into extension methods to accomplish this:
public static class XmlExtensions
{
public static T Deserialize<T>(this XContainer element, XmlSerializer serializer = null)
{
using (var reader = element.CreateReader())
{
object result = (serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
if (result is T)
return (T)result;
}
return default(T);
}
public static XElement SerializeToXElement<T>(this T obj, XmlSerializer serializer = null)
{
var doc = new XDocument();
using (var writer = doc.CreateWriter())
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(writer, obj);
var element = doc.Root;
if (element != null)
element.Remove();
return element;
}
public static XName ContainerElementName { get { return (XName)"Object"; } }
public static XName ContainerAttributeName { get { return (XName)"Name"; } }
public static XElement SetItem<T>(this XDocument doc, string attributeValue, T obj)
{
return doc.SetItem(ContainerElementName, ContainerAttributeName, attributeValue, obj);
}
static XElement SetItem<T>(this XDocument doc, XName containerElementName, XName containerAttributeName, string attributeValue, T obj)
{
if (doc == null || containerElementName == null || containerAttributeName == null || attributeValue == null)
throw new ArgumentNullException();
if (doc.Root == null)
throw new ArgumentException("doc.Root == null");
var container = doc.Root.Elements(containerElementName).Where(e => (string)e.Attribute(containerAttributeName) == attributeValue).SingleOrDefault();
if (container == null)
{
container = new XElement(containerElementName, new XAttribute(containerAttributeName, attributeValue));
doc.Root.Add(container);
}
else
{
// Remove old content.
container.RemoveNodes();
}
var element = obj.SerializeToXElement();
container.Add(element);
return element;
}
public static T GetItem<T>(this XDocument doc, string attributeValue)
{
return doc.GetItem<T>(ContainerElementName, ContainerAttributeName, attributeValue);
}
static T GetItem<T>(this XDocument doc, XName containerElementName, XName containerAttributeName, string attributeValue)
{
if (doc == null || containerElementName == null || containerAttributeName == null || attributeValue == null)
throw new ArgumentNullException();
if (doc.Root == null)
throw new ArgumentException("doc.Root == null");
var container = doc.Root.Elements(containerElementName).Where(e => (string)e.Attribute(containerAttributeName) == attributeValue).SingleOrDefault();
if (container == null)
return default(T);
var element = container.Elements().SingleOrDefault();
if (element == null)
return default(T);
return element.Deserialize<T>();
}
public static XDocument CreateDefaultXDocument()
{
var xml = #"<StoredObjects></StoredObjects>";
return XDocument.Parse(xml);
}
}
Now you can do
doc.AddItem(MYOBJECT, objectName);
And later
var MYOBJECT2 = doc.GetItem<MYOBJECT_TYPE>(objectName);
Example fiddle.
Related
I want to compare a FlowDocument to a document of Rich Text Box. Here is the code
if (rtbEditor.Document != (XamlReader.Parse(currentNote.content) as FlowDocument))
{
MessageBox.Show("Overwrite existing Note?", "Save", MessageBoxButton.OKCancel);
}
At the beginning I set rtbEditor's document as
rtbEditor.Document = XamlReader.Parse(currentNote.content) as FlowDocument;
Thus, unless the content of rtbEditor is changed, I thought that the if statement should not execute,but it does. Probably this is not the way to compare FlowDocuments. If this is not the correct way then how can we compare two documents?
If it is necessary, the currentNote.content is a string containing xml content of FlowDocument.
Assuming you have no images in your FlowDocument instances, you can just serialize to XAML and compare the XAML. First, create extension methods to generate the XAML strings:
public static class FrameworkContentElementExtensions
{
public static string ToXaml(this FrameworkContentElement element) // For instance, a FlowDocument
{
if (element == null)
return null;
var sb = new StringBuilder();
using (var xmlWriter = XmlWriter.Create(sb))
{
XamlWriter.Save(element, xmlWriter);
}
return sb.ToString();
}
public static string ToFormattedXamlString(this FrameworkContentElement element)
{
if (element == null)
return null;
var settings = new XmlWriterSettings() { Indent = true, IndentChars = " " };
var sb = new StringBuilder();
using (var xmlWriter = XmlWriter.Create(sb, settings))
{
XamlWriter.Save(element, xmlWriter);
}
return sb.ToString();
}
}
Then you can do
if (rtbEditor.Document.ToXaml() != currentNote.content)
{
MessageBox.Show("Overwrite existing Note?", "Save", MessageBoxButton.OKCancel);
}
Note that if the XAML differs only because of cosmetic formatting (XML indentation), since XAML documents are valid XML, you can parse your XAML to an XElement and use XNode.DeepEquals(). You can also serialize a FrameworkContentElement directly to an XElement without the intervening string representation for improved performance:
public static class FrameworkContentElementExtensions
{
public static XElement ToXamlXElement(this FrameworkContentElement element) // For instance, a FlowDocument
{
if (element == null)
return null;
var doc = new XDocument();
using (var xmlWriter = doc.CreateWriter())
{
XamlWriter.Save(element, xmlWriter);
}
var xElement = doc.Root;
if (xElement != null)
xElement.Remove();
return xElement;
}
}
And then
var docXaml = rtbEditor.Document.ToXamlXElement();
var currentNoteXaml = XElement.Parse(currentNote.content);
if (!XNode.DeepEquals(docXaml, currentNoteXaml))
{
MessageBox.Show("Overwrite existing Note?", "Save", MessageBoxButton.OKCancel);
}
If you are concerned there might be embedded messages and want to generate a warning message in this case, see Finding all Images in a FlowDocument.
hi I want to control a xml file... for this i use linq to xml.
private string GetGroup(string xml, string id)
{
XDocument document;
XElement element;
try
{
document = XDocument.Load(xml);
//element = document.Root.Elements("Permissiongroup").FirstOrDefault(e => e.Element("id").Value == id);
element = document.Elements("Permissiongroup").FirstOrDefault(e => e.Element("id").Value == id);
if (element != null)
{
return element.Element("display").Value;
}
else
{
return string.Empty;
}
}
catch (Exception)
{
return null;
}
finally
{
document = null;
element = null;
}
}
here is my xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<Permissiongroup>
<Permission id="Hessen" display="KV-IT" />
<Permission id="Berlin" display="DBG_Update" />
</Permissiongroup>
For example i want if the method is ..
string group = GetGroup(xmlpath, "Hessen");
group is "KV-IT"
there are a few things wrong with what you currently have - you're missing Permission from the query and looking for an element instead of an attribute. The following works, albeit I would split it down to check for the existence of elements (e.g. make sure there is a Permission element, etc.) rather than relying on error handling.
// string group = GetGroup(xmlpath, "Hessen"); // returns KV-IT
// string group2 = GetGroup(xmlpath, "Berlin"); //DBG_Update
private string GetGroup(string xml, string id)
{
XDocument document;
XElement element;
try
{
document = XDocument.Load(xml);
element = document.Elements("Permissiongroup").Elements(("Permission")).FirstOrDefault(t => t.Attribute("id").Value == id);
if (element != null)
{
return element.Attribute("display").Value;
}
else
{
return string.Empty;
}
}
catch (Exception ex)
{
return null;
}
finally
{
document = null;
element = null;
}
}
Use method Attribute() instead of using Element() to access attributes
private string GetGroup(string xml, string id)
{
XDocument document;
XElement element;
try
{
document = XDocument.Load(xml);
//element = document.Root.Elements("Permissiongroup").FirstOrDefault(e => e.Attribute("id").Value == id);
element = document.Elements("Permissiongroup").FirstOrDefault(e => e.Attribute("id").Value == id);
if (element != null)
{
return element.Attribute("display").Value;
}
else
{
return string.Empty;
}
}
catch (Exception)
{
return null;
}
finally
{
document = null;
element = null;
}
}
You can write your xml structure and then can convert xml in to xsd using
http://www.freeformatter.com/xsd-generator.html#ad-output.
Once you have xsd file , you can download jaxb ,which will convert xsd file in to POJO file
and then in your program you can access attributes of an xml like this
JAXBContext jc2 = JAXBContext.newInstance(someclassname.class);
File xml2 = new File(xml_File);
Unmarshaller unmarshaller2 = jc2.createUnmarshaller();
someclassnameObject= (someclassname) unmarshaller2.unmarshal(xml2);
and can use object to use its attributes e.g someclassnameObject.attribute
i have one xml string like this
string stxml="<Status>Success</Status>";
I also creaated one xml document
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode rootNode = doc.CreateElement("StatusList");
doc.AppendChild(rootNode);
i need an output like this.
<StatusList>
<Status>Success</Status>
</StatusList>
How can i achieve this.if we using innerhtml,it will insert.But i want to insert xml string as a xmlnode itself
A very simple way to achieve what you are after is to use the often overlooked XmlDocumentFragment class:
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode rootNode = doc.CreateElement("StatusList");
doc.AppendChild(rootNode);
//Create a document fragment and load the xml into it
XmlDocumentFragment fragment = doc.CreateDocumentFragment();
fragment.InnerXml = stxml;
rootNode.AppendChild(fragment);
Using Linq to XML:
string stxml = "<Status>Success</Status>";
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("StatusList", XElement.Parse(stxml)));
You could instead use the XElement class:
string stxml = "<Status>Success</Status>";
var status = XElement.Parse(stxml);
var statusList = new XElement("StatusList", status);
var output = statusList.ToString(); // looks as you'd like
If you want to write the new statusList content to a file:
statusList.Save(#"C:\yourFile.xml", SaveOptions.None);
you ca try it using xmlwriter
using (XmlWriter writer = XmlWriter.Create("new.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("StatusList");
writer.WriteElementString("Status", "Success"); // <-- These are new
writer.WriteEndDocument();
}
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using System.ComponentModel;
public class MyClass
{
public static void RunSnippet()
{
XmlNode node = default(XmlNode);
if(node == null)
Console.WriteLine(bool.TrueString);
if(node != null)
Console.WriteLine(bool.FalseString);
XmlDocument doc = new XmlDocument();
node = doc.CreateNode (XmlNodeType.Element,"Query", string.Empty);
node.InnerXml=#"<Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where>";
string xmlData = ToXml<XmlNode>(node);
Console.WriteLine(xmlData);
XmlNode node1 = ConvertFromString(typeof(XmlNode), #"<Query><Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where></Query>") as XmlNode;
if(node1 == null)
Console.WriteLine(bool.FalseString);
if(node1 != null)
Console.WriteLine(bool.TrueString);
string xmlData1 = ToXml<XmlNode>(node1);
Console.WriteLine(xmlData1);
}
public static string ToXml<T>(T t)
{
string Ret = string.Empty;
XmlSerializer s = new XmlSerializer(typeof(T));
using (StringWriter Output = new StringWriter(new System.Text.StringBuilder()))
{
s.Serialize(Output, t);
Ret = Output.ToString();
}
return Ret;
}
public static object ConvertFromString(Type t, string sourceValue)
{
object convertedVal = null;
Type parameterType = t;
if (parameterType == null) parameterType = typeof(string);
try
{
// Type t = Type.GetType(sourceType, true);
TypeConverter converter = TypeDescriptor.GetConverter(parameterType);
if (converter != null && converter.CanConvertFrom(typeof(string)))
{
convertedVal = converter.ConvertFromString(sourceValue);
}
else
{
convertedVal = FromXml(sourceValue, parameterType);
}
}
catch { }
return convertedVal;
}
public static object FromXml(string Xml, Type t)
{
object obj;
XmlSerializer ser = new XmlSerializer(t);
using (StringReader stringReader = new StringReader(Xml))
{
using (System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stringReader))
{
obj = ser.Deserialize(xmlReader);
}
}
return obj;
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
From my experience it is always better to work with unique id's i suggest u look in that situation first and then come back to this page, and research/position my code for a try if u did not yet have a solution for it.
I just finished it on my side for my own project, i have modified abit to look more integrated for your project.
Good luck. Sorry for the late response ;-)
XmlDocument xDoc = new XmlDocument();
string Bingo = "Identification code";
xDoc.Load(pathFile);
XmlNodeList idList = xDoc.GetElementsByTagName("id");
XmlNodeList statusList = xDoc.GetElementsByTagName("Status");
for (int i = 0; i < idList.Count; i++)
{
StatusNode = "<Status>fail</Status>";
XmlDocumentFragment fragment = xDoc.CreateDocumentFragment();
fragment.InnerXml = StatusNode;
statusList[i].InnerXml = "";
statusList[i].AppendChild(fragment);
if (statusList[i].InnerText == Bingo)
{
StatusNode = "<Status>Succes!</Status>";
fragment.InnerXml = Status;
statusList[i].InnerXml = "";
statusList[i].AppendChild(fragment);
}
}
xDoc.Save(pathFile);
In my XML, I have an element like
<myElement myAttribute="myNamespacePrefix:foobar"/>
There is a xmlns declaration in this file:
<xmlns:myNamespacePrefix="http://www.mydomain.com/blah#"/>
I want to make a URI from the value of myAttribute.
IE, I want:
http://www.mydomain.com/blah#foobar
Sounds simple, doesn't it?
So what is the simple solution?
I couldn't find anything in the Framework or on MSDN, so I just had to write this no-doubt bug-ridden mess:
public static Uri GetUri(string value, XElement containingElement)
{
if (value.Contains(":"))
{
var prefix = String.Concat(value.TakeWhile(c => c != ':'));
XNamespace ns;
if (containingElement.GetNamespaceOfPrefix(prefix) != null)
ns = containingElement.GetNamespaceOfPrefix(prefix);
else ns = containingElement.GetDefaultNamespace();
var localName = String.Concat(value.SkipWhile(c => c != ':').Skip(1));
Contract.Assert(ns != null);
if (String.IsNullOrWhiteSpace(ns.NamespaceName))
return new Uri(value);
else
{
StringBuilder sb = new StringBuilder(ns.NamespaceName);
if (!ns.NamespaceName.EndsWith("#"))
{
sb.Append('#');
}
return new Uri(String.Format(CultureInfo.CurrentCulture, "{0}{1}", sb.ToString(), localName));
}
}
else // no namespace prefix
{
String fragment;
if (!value.StartsWith("#"))
fragment = value.Insert(0, "#");
else
fragment = value;
var uriString = String.Format(CultureInfo.CurrentCulture, "{0}{1}", containingElement.BaseUri, fragment);
return new Uri(uriString);
}
}
I have done some XML-related stuff recently. This is an example of what I used to get information from the XML in C#:
XmlDocument XML = new XmlDocument();
XML.LoadXml(/* XML data in string format goes here */);
Then you can do things like the following:
if (XML["myElement"] != null)
{
string myAttributeStr = XML["myElement"].Attributes["myAttribute"].Value.ToString().Trim();
}
public void AddNodeToXml(string helpid, string fileName)
{
const string STR_EXPRESSION = "/Form/Controls/Control";
XPathDocument doc = null;
try
{
doc = new XPathDocument(fileName);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
if (doc != null)
{
XPathNavigator navigator = doc.CreateNavigator();
XPathNodeIterator localIterator = navigator.Select(STR_EXPRESSION);
while (localIterator.MoveNext())
{
if (localIterator.Current != null)
{
if (localIterator.Current.Name.Equals("Control"))
{
localIterator.Current.MoveToFirstAttribute();
if (localIterator.Current.Value.Equals(helpid))
{
localIterator.Current.MoveToParent();
localIterator.Current.CreateAttribute(string.Empty, "NewAttribute", string.Empty, "value");
}
}
}
}
}
}
My xml structure is as show in STR_EXPRESSION
I want to add new attribute to the control node if currnet cotrol name attribute value is "helpid",I tried using CreateAttribute() this method but it gives an exception as System.NotSupportedException.
this would be so much easier with Linq to XML, is there any reason you aren't using it?
This is untested code I wrote off the top of my head, but it should be pretty close, it shows how you would use Linq to solve the same problem:
XElement root = XDocument.Load(fileName).Root; //get the root element of the XML document
foreach (var controlElement in root.Descendants("Control").Where(c=>c.Attributes[0] != null && c.Attributes[0].value == helpId)) //get all of the control elements with the appropriate helpid value
{
if (controlElement.Parent == null) continue; // it's always good to be defensive
controlElement.Parent.Attributes.Add("NewAttribute", string.Empty);
}