Ok, so I've got a type:
public class MonitorConfiguration
{
private string m_sourcePath;
private string m_targetPath;
public string TargetPath
{
get { return m_targetPath; }
set { m_targetPath = value; }
}
public string SourcePath
{
get { return m_sourcePath; }
set { m_sourcePath = value; }
}
//need a parameterless constructor, just for serialization
private MonitorConfiguration()
{
}
public MonitorConfiguration(string source, string target)
{
m_sourcePath = source;
m_targetPath = target;
}
}
When I serialise and deserialise a list of these, like this
XmlSerializer xs = new XmlSerializer(typeof(List<MonitorConfiguration>));
using (Stream isfStreamOut = isf.OpenFile("Test1.xml", FileMode.Create))
{
xs.Serialize(isfStreamOut, monitoringPaths);
}
using (Stream isfStreamIn = isf.OpenFile("Test1.xml", FileMode.Open))
{
monitoringPaths = xs.Deserialize(isfStreamIn) as List<MonitorConfiguration>;
}
everything works fine.
However, I really want to hide the public setters of the attributes. This prevents them from being serialised by the XML serialiser. So, I implement my own, like this:
Change the class declaration to this:public class MonitorConfiguration : IXmlSerializable
and add these:
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
//make sure we read everything
while (reader.Read())
{
//find the first element we care about...
if (reader.Name == "SourcePath")
{
m_sourcePath = reader.ReadElementString("SourcePath");
m_targetPath = reader.ReadElementString("TargetPath");
// return;
}
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteElementString("SourcePath", m_sourcePath);
writer.WriteElementString("TargetPath", m_targetPath);
}
This seems to work, however, I only ever get the first item from the list out, all the others are forgotten. I've tried with and without the return that's currently commented out. What am I doing wrong here?
It should be noted that this is just a snippet code that illustrates the problem; I'm limited to which XML serialisation technology I'm using my an eternal mechanic.
This CodeProject article explains how to get around a few pitfalls when working with IXmlSerializable.
Specifically, you probably need to call reader.ReadEndElement(); when you've found all your elements in ReadXml (see the section How to Implement ReadXml? in the article).
Related
I have a class implementing IXmlSerializable. This class contains a few properties. Serializing and Deserializing a single instance of the class works fine. But in case of collection of the class, Serialization works fine but Deserialization runs forever. Here is a code snippet. I am using .Net 4.6.2.
public class MyClass : IXmlSerializable
{
public int A { get; set; }
public int B { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
this.A = Convert.ToInt32(reader.GetAttribute("A"));
this.B = Convert.ToInt32(reader.GetAttribute("B"));
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("A", this.A.ToString());
writer.WriteAttributeString("B", this.B.ToString());
}
}
class Program
{
static void Main(string[] args)
{
var instance = new MyClass { A = 1, B = 2 };
Serialize(instance);
instance = Deserialize<MyClass>();//works fine
var list = new List<MyClass> { new MyClass { A = 10, B = 20 } };
Serialize(list);
list = Deserialize<List<MyClass>>();//runs forever
}
private static void Serialize(object o)
{
XmlSerializer ser = new XmlSerializer(o.GetType());
using (TextWriter writer = new StreamWriter("xml.xml", false, Encoding.UTF8))
{
ser.Serialize(writer, o);
}
}
private static T Deserialize<T>()
{
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextReader reader = new StreamReader("xml.xml"))
{
return (T)ser.Deserialize(reader);
}
}
}
Your problem is that, as explained in the documentation, ReadXml() must consume its wrapper element as well as its contents:
The ReadXml method must reconstitute your object using the information that was written by the WriteXml method.
When this method is called, the reader is positioned on the start tag that wraps the information for your type. That is, directly on the start tag that indicates the beginning of a serialized object. When this method returns, it must have read the entire element from beginning to end, including all of its contents. Unlike the WriteXml method, the framework does not handle the wrapper element automatically. Your implementation must do so. Failing to observe these positioning rules may cause code to generate unexpected runtime exceptions or corrupt data.
MyClass.ReadXml() is not doing this, which causes an infinite loop when the MyClass object is not serialized as the root element. Instead, your MyClass must look something like this:
public class MyClass : IXmlSerializable
{
public int A { get; set; }
public int B { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
/*
* https://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.readxml.aspx
*
* When this method is called, the reader is positioned at the start of the element that wraps the information for your type.
* That is, just before the start tag that indicates the beginning of a serialized object. When this method returns,
* it must have read the entire element from beginning to end, including all of its contents. Unlike the WriteXml method,
* the framework does not handle the wrapper element automatically. Your implementation must do so. Failing to observe these
* positioning rules may cause code to generate unexpected runtime exceptions or corrupt data.
*/
var isEmptyElement = reader.IsEmptyElement;
this.A = XmlConvert.ToInt32(reader.GetAttribute("A"));
this.B = XmlConvert.ToInt32(reader.GetAttribute("B"));
reader.ReadStartElement();
if (!isEmptyElement)
{
reader.ReadEndElement();
}
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("A", XmlConvert.ToString(this.A));
writer.WriteAttributeString("B", XmlConvert.ToString(this.B));
}
}
Now your <MyClass> element is very simple with no nested or optional elements. For more complex custom serializations there are a couple of strategies you could adopt to guarantee that your ReadXml() method reads exactly as much as it should, no more and no less.
Firstly, you could call XNode.ReadFrom() to load the current element into an XElement. This requires a bit more memory than parsing directly from an XmlReader but is much easier to work with:
public class MyClass : IXmlSerializable
{
public int A { get; set; }
public int B { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
var element = (XElement)XNode.ReadFrom(reader);
this.A = (int)element.Attribute("A");
this.B = (int)element.Attribute("B");
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("A", XmlConvert.ToString(this.A));
writer.WriteAttributeString("B", XmlConvert.ToString(this.B));
}
}
Secondly, you could use XmlReader.ReadSubtree() to ensure the required XML content is consumed:
public class MyClass : IXmlSerializable
{
public int A { get; set; }
public int B { get; set; }
public XmlSchema GetSchema()
{
return null;
}
protected virtual void ReadXmlSubtree(XmlReader reader)
{
this.A = XmlConvert.ToInt32(reader.GetAttribute("A"));
this.B = XmlConvert.ToInt32(reader.GetAttribute("B"));
}
public void ReadXml(XmlReader reader)
{
// Consume all child nodes of the current element using ReadSubtree()
using (var subReader = reader.ReadSubtree())
{
subReader.MoveToContent();
ReadXmlSubtree(subReader);
}
reader.Read(); // Consume the end element itself.
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("A", XmlConvert.ToString(this.A));
writer.WriteAttributeString("B", XmlConvert.ToString(this.B));
}
}
A few final notes:
Be sure to handle both <MyClass /> and <MyClass></MyClass>. These two forms are semantically identical and a sending system could chose either.
Prefer the methods from the XmlConvert class to convert primitives from and to XML. Doing so handles internationalization correctly.
Be sure to test with and without indentation. Sometimes a ReadXml() method will consume an extra XML node but the bug will be hidden when indentation is enabled -- as it is the whitespace node that gets eaten.
For further reading see How to Implement IXmlSerializable Correctly.
I cannot find a reason why properties without getters aren't parsing properly, let me write you an example:
For XML in format
<request>
<job
mode="modefirst"
/>
<request>
I am trying to deserialize it to the POCO with a property:
private ESomeEnum emode;
[XmlAttribute(AttributeName = "mode")]
public string Mode
{
set { ESomeEnum.TryParse( blah blah );
}
emode is being set for default value in class constructor, while deserializing (System.Xml.Serialization without custom classes, just trying to be minimalistic in here) the xml from above, the setter is never being called, but when property 'Mode' contains a getter
get { return this.emode.ToString(); }
setter is actually being hit and proper value set during deserialization.
Why this situation occurs? Is there any reason behind it?
The XmlSerializer processes properties only, which have public get-set accessors. But you can customize anything by implementing IXmlSerializable:
public class MyXmlSerializableClass : IXmlSerializable
{
private ESomeEnum emode = ESomeEnum.modefirst;
public string Mode
{
set { emode = ESomeEnum.Parse(value); }
}
public int ReadWriteProperty { get; set; }
public int SemiReadOnlyProperty { get; private set; }
private int backingFieldOfRealReadOnlyProperty;
public int RealReadOnlyProperty
{
get { return backingFieldOfRealReadOnlyProperty; }
}
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(XmlReader reader)
{
if (reader.Settings != null && !reader.Settings.IgnoreWhitespace)
{
reader = XmlReader.Create(reader, new XmlReaderSettings { IgnoreWhitespace = true });
reader.Read();
}
reader.ReadStartElement();
Mode = reader.ReadElementContentAsString("Mode", String.Empty);
ReadWriteProperty = reader.ReadElementContentAsInt("ReadWriteProperty", String.Empty);
SemiReadOnlyProperty = reader.ReadElementContentAsInt("ReadOnlyAutoProperty", String.Empty);
backingFieldOfRealReadOnlyProperty = reader.ReadElementContentAsInt("ReadOnlyProperty", String.Empty);
}
public void WriteXml(XmlWriter writer)
{
writer.WriteElementString("Mode", emode.ToString());
writer.WriteElementString("ReadWriteProperty", ReadWriteProperty.ToString(CultureInfo.InvariantCulture));
writer.WriteElementString("ReadOnlyAutoProperty", SemiReadOnlyProperty.ToString(CultureInfo.InvariantCulture));
writer.WriteElementString("ReadOnlyProperty", RealReadOnlyProperty.ToString(CultureInfo.InvariantCulture));
}
#endregion
internal MyXmlSerializableClass()
{/*needed for deserialization*/
}
}
I have a problem with implementing an IsDirty mechanism with my XmlSerializer system.
This is how my serialization is called:
public OCLMEditorModel()
{
DeSerialize();
}
public void Serialize()
{
XmlSerializer x = new XmlSerializer(_ModelData.GetType());
using (StreamWriter writer = new StreamWriter(_strPathModelDataXml))
{
x.Serialize(writer, _ModelData);
}
}
public void DeSerialize()
{
_ModelData = new OCLMModelData();
XmlSerializer x = new XmlSerializer(_ModelData.GetType());
using (StreamReader reader = new StreamReader(_strPathModelDataXml))
{
_ModelData = (OCLMModelData)x.Deserialize(reader);
}
}
It reads and saves perfectly, no issues there. But it is the IsDirty flag I have issues with. Directly after the DeSerialize call ...
Ass the IsDirty are set to true. Even though all we have done is read it in from the computer. Example properties:
public class MaterialItem
{
[XmlAttribute]
public string Setting
{
get { return _Setting; }
set
{
_Setting = value;
MarkDirty();
}
}
private string _Setting;
[XmlText]
public string Material
{
get { return _Material; }
set
{
_Material = value;
MarkDirty();
}
}
private string _Material;
[XmlIgnore]
public bool IsDirty { get { return _isDirty; } }
private bool _isDirty;
public void MarkClean()
{
_isDirty = false;
}
protected void MarkDirty()
{
_isDirty = true;
}
public MaterialItem()
{
MarkClean();
}
}
Ideally, the flag should be false when we have just read it using XMLSerializer.
What am I doing wrong?
Thank you.
XmlSerializer doesn't work in any mysterious way.
It uses reflection, yes, but only to get the properties it can serialize/deserialize. Then it uses those properties to get/set the required values.
So during the deserialization those setters will be called, thus calling the MarkDirty method, thus marking your entities dirty.
There isn't anything you can change in XmlSerializer, but you can change your deserialization method, so it sets the entity clean just after deserializing it:
public void DeSerialize()
{
_ModelData = new OCLMModelData();
XmlSerializer x = new XmlSerializer(_ModelData.GetType());
using (StreamReader reader = new StreamReader(_strPathModelDataXml))
{
_ModelData = (OCLMModelData)x.Deserialize(reader);
}
_ModelData.MarkClean();
}
I am creating an Automation Framework using Selenium C#, currently I am working on the object repository part. So I would like to know what all types of files I can use as the Object Repository.Currently I am thinking of using either XML or Excel but I am not sure which one is better performance wise, so can any of you share your views on this and also let me know if there are any other options.
I am planning to use XmlDocument for reading xml and oledb connection for reading excel.
By Object repository i think you mean different elements, their locators and some other required attributes, because As far as i know selenium do not have concept of Object Repository inherently.
If so, you need to think about who is going to maintain this Repository,
with few thousand locators, rather than performance maintainability would be a major issue.
Also, think about making it isolated by implementing an interface, so in future if you decide to change implementation because of any issue, it will be not impact your framework.
And XML, Excel, text file(with any delimiter), a Database, json file are good contenders for this.
Selenium does not work with XML pages by default, as browsers do not show XML files as XML, but show its as converted to html files.
For the task I had used following code (its based on HTMLAgilityPack):
XmlActions.cs
namespace BotAgent.Ifrit.Core.Xml
{
using HtmlAgilityPack;
public partial class XmlActions
{
private HtmlDocument _xmlDoc;
private HtmlNode _rootNode;
public XmlActions()
{
_xmlDoc = new HtmlDocument();
}
private void Update()
{
string pageSource = Brwsr.CurrPage.PageSource.Replace("\r\n", string.Empty);
_xmlDoc.LoadHtml(pageSource);
_rootNode = _xmlDoc.DocumentNode;
}
public NodeSingle Elmnt(string xpath)
{
Update();
var currNode = _rootNode.SelectSingleNode(xpath);
return new NodeSingle(currNode);
}
public NodesMultiple Elmnts(string xpath)
{
Update();
var nodesGroup = _rootNode.SelectNodes(xpath);
return new NodesMultiple(nodesGroup);
}
}
}
XmlActions.NodeSingle.cs
using System;
namespace BotAgent.Ifrit.Core.Xml
{
using HtmlAgilityPack;
partial class XmlActions
{
public class NodeSingle
{
private readonly HtmlNode _currNode;
public string Text
{
get
{
return CleanUpStringFromXml(_currNode.InnerText);
}
}
public string TagName
{
get
{
return _currNode.OriginalName;
}
}
public string XmlInner
{
get
{
return _currNode.InnerHtml;
}
}
public string XmlOuter
{
get
{
return _currNode.OuterHtml;
}
}
public NodeSingle(HtmlNode currentNode)
{
_currNode = currentNode;
}
public bool Exist()
{
if (_currNode == null)
{
return false;
}
return true;
}
public bool AttributesExist()
{
return _currNode.HasAttributes;
}
public bool AttributeExist(string attributeName)
{
if (_currNode.Attributes[attributeName] != null)
{
return true;
}
return false;
}
public string AttributeValue(string attrName)
{
return _currNode.GetAttributeValue(attrName, string.Empty);
}
public bool HaveChildren()
{
var firstChildNode = _currNode.FirstChild;
if (firstChildNode != null)
{
return true;
}
return false;
}
public NodeSingle FirstChild()
{
HtmlNode node = null;
try
{
node = _currNode.ChildNodes[1];
}
catch (Exception)
{
//// No need to throw exception, its normal if there are no child
}
return new NodeSingle(node);
}
public NodeSingle Parent()
{
return new NodeSingle(_currNode.ParentNode);
}
private string CleanUpStringFromXml(string xml)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(xml);
var root = doc.DocumentNode;
root.RemoveAllChildren();
return root.OuterHtml.Replace(" ", string.Empty);
}
}
}
}
XmlActions.NodesMultiple
namespace BotAgent.Ifrit.Core.Xml
{
using System.Collections.Generic;
using System.Linq;
using HtmlAgilityPack;
partial class XmlActions
{
public class NodesMultiple
{
private readonly HtmlNodeCollection _nodesGroup;
public int Count
{
get
{
return _nodesGroup.Count;
}
}
public NodesMultiple(HtmlNodeCollection nodesGroup)
{
this._nodesGroup = nodesGroup;
}
public NodeSingle GetElementByIndex(int index)
{
var singleNode = _nodesGroup.ElementAt(index);
return new NodeSingle(singleNode);
}
public List<NodeSingle> GetAll()
{
return _nodesGroup.Select(node => new NodeSingle(node)).ToList();
}
}
}
}
I had used my own framework code here, but this must not create problem for you to change it to clear selenium code.
After this you can create static XML var with browser instance and use like this:
bool isIdExist = Brwsr.Xml.Elem(".//div[1]").AttributeExist("id");
or
bool haveChild = Brwsr.Xml.Elem(".//div[1]").FirstChild().Exist;
I'm trying out Generics and I had this (not so) great idea of creating an XMLSerializer class. The code I pieced together is below:
public class Persist<T>
{
private string _path;
public Persist(string path) {
this._path = path;
}
public void save(T objectToSave)
{
XmlSerializer s = new XmlSerializer(typeof(T));
TextWriter w = new StreamWriter(this._path);
try { s.Serialize(w, objectToSave); }
catch (InvalidDataException e) { throw e; }
w.Close(); w.Dispose();
}
public T load()
{
XmlSerializer s = new XmlSerializer(typeof(T));
TextReader r = new StreamReader(this._path);
T obj;
try { obj = (T)s.Deserialize(r); }
catch (InvalidDataException e) { throw e; }
r.Close(); r.Dispose();
return obj;
}
}
Here's the problem: It works fine on Persist<List<string>> or Persist<List<int>> but not on Persist<List<userObject>> or any other custom (but serializable) objects. userObject itself is just a class with two {get;set;} properties, which I have serialized before.
I'm not sure if the problems on my Persist class (generics), XML Serialization code, or somewhere else :( Help is very much appreciated~
Edit:
code for userObject
public class userObject
{
public userObject(string id, string name)
{
this.id = id;
this.name = name;
}
public string id { get;private set; }
public string name { get;set; }
}
Looks to me like your code should just work - even though it does have a few flaws.
EDIT: Your userObject class isn't serializable. Xml serialization only works on types with a public, parameterless constructor - the current class won't work. Also, you should really rewrite your code to avoid explicit calls to .Close() or .Dispose() and instead prefer using where possible - as is, you might get random file locking if at any point during serialization an error occurs and your method terminates by exception - and thus doesn't call .Dispose().
Personally, I tend to use a just-for-serialization object hierarchy that's just a container for data stored in xml and avoids any behavior - particularly side effects. Then you can use a handly little base class that makes this simple.
What I use in my projects is the following:
public class XmlSerializableBase<T> where T : XmlSerializableBase<T>
{
static XmlSerializer serializer = new XmlSerializer(typeof(T));
public static T Deserialize(XmlReader from) { return (T)serializer.Deserialize(from); }
public void SerializeTo(Stream s) { serializer.Serialize(s, this); }
public void SerializeTo(TextWriter w) { serializer.Serialize(w, this); }
public void SerializeTo(XmlWriter xw) { serializer.Serialize(xw, this); }
}
...which caches the serializer in a static object, and simplifies usage (no generic type-paramenters needed at call-locations.
Real-life classes using it:
public class ArtistTopTracks {
public string name;
public string mbid;//always empty
public long reach;
public string url;
}
[XmlRoot("mostknowntracks")]
public class ApiArtistTopTracks : XmlSerializableBase<ApiArtistTopTracks> {
[XmlAttribute]
public string artist;
[XmlElement("track")]
public ArtistTopTracks[] track;
}
Sample serialization calls:
using (var xmlReader = XmlReader.Create([...]))
return ApiArtistTopTracks.Deserialize(xmlReader);
//[...]
ApiArtistTopTracks toptracks = [...];
toptracks.SerializeTo(Console.Out);
There can be a number of reasons why your code fails: This text is particularly helpful when having issues: Troubleshooting Common Problems with the XmlSerializer . Maybe you have some type hierarchy in your user objects and the serializer does not know about it?