I am trying to deserialize an XML file to a class. The XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<locations>
<location id="0">
<name>park</name>
<temperature>5</temperature>
<wind>26</wind>
<weather_text_SI>sunny</weather_text_SI>
<visibility></visibility>
<latitude>46.4527</latitude>
<longitude>15.334</longitude>
<elevation>1517</elevation>
</location>
</locations>
The class that I want to deserialize it to is:
[XmlRootAttribute("locations")]
public class SnowPark
{
public SnowPark()
{
}
private int id;
[XmlAttribute("id")]
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
[XmlElement("name")]
public string Name
{
get { return name; }
set { name = value; }
}
private int temperature;
[XmlElement("temperature")]
public int Temperature
{
get { return temperature; }
set { temperature = value; }
}
private int wind;
[XmlElement("wind")]
public int Wind
{
get { return wind; }
set { wind = value; }
}
private string weatherText;
[XmlElement("weather_text_SI")]
public string WeatherText
{
get { return weatherText; }
set { weatherText = value; }
}
private double latitude;
[XmlElement("latitude")]
public double Latitude
{
get { return latitude; }
set { latitude = value; }
}
private double longitude;
[XmlElement("longitude")]
public double Longitude
{
get { return longitude; }
set { longitude = value; }
}
private int elevation;
[XmlElement("elevation")]
public int Elevation
{
get { return elevation; }
set { elevation = value; }
}
}
I try to deserialize the XML file
XmlSerializer deserializer = new XmlSerializer(typeof(List<SnowPark>));
TextReader textReader = new StreamReader(#"file.xml");
List<SnowPark> parks;
parkss = (List<SnowPark>)deserializer.Deserialize(textReader);
textReader.Close();
However I get an exception:
There is an error in XML document (2, 2).
and an inner exception:
<locations xmlns=''> was not expected.
No luck finding the solution so far. Help appreciated.
The XmlRootAttribute doesn't apply since you are serialising a list of then, not an individual item; this also means your XML is one layer further-out than needed.
IMO, your easiest option here is:
[XmlRoot("locations")]
public class Locations
{
[XmlElement("location")]
public List<SnowPark> Parks {get;set;}
}
and deserialize a Locations object, using typeof(Locations) to initialisers the XmlSerializer
Related
I am developing an UWP app using the Creators Update SDK.
I am trying to serialize a ObservableCollection ignoring a property of their class.
Here is my code, it have my class and the methods to serialize, you can see I am using [DataContract] and [IgnoreDataMember] but it's not working.
public class Classes
{
[DataContract]
public class Car : BindableBase
{
[DataMember]
private string _Name;
public string Name
{
get { return _Name; }
set { Set(ref _Name, value); }
}
[DataMember]
private string _Brand;
public string Brand
{
get { return _Brand; }
set { Set(ref _Brand, value); }
}
[IgnoreDataMember]
private bool _Electric;
public bool Electric
{
get { return _Electric; }
set { Set(ref _Electric, value); }
}
[DataMember]
private double _Price;
public double Price
{
get { return _Price; }
set { Set(ref _Price, value); }
}
}
public class Values_Car : ObservableCollection<Car> { }
public static class Garage
{
public static Values_Car Cars = new Values_Car();
static Garage()
{
}
}
[XmlRoot("Root")]
public class GarageDTO
{
[XmlElement]
public Values_Car Cars { get { return Garage.Cars; } }
}
}
public class NewSerialization
{
private static void FillList()
{
Car e_1 = new Car()
{
Name = "element_Name",
Brand = "element_Brand",
Electric = true,
Price = 1,
};
Car e_2 = new Car()
{
Name = "element_Name",
Brand = "element_Brand",
Electric = true,
Price = 2,
};
Garage.Cars.Add(e_1);
Garage.Cars.Add(e_2);
}
public static string Serializer()
{
FillList();
var _Instance = new GarageDTO();
var serializer = new XmlSerializer(typeof(GarageDTO));
using (var stream_original = new MemoryStream())
{
serializer.Serialize(stream_original, _Instance);
string string_original = string.Empty;
stream_original.Position = 0;
using (StreamReader reader = new StreamReader(stream_original, Encoding.Unicode))
{
string_original = reader.ReadToEnd();
}
return string_original;
}
}
}
using NewSerialization.Serializer(); I got:
But in the xml I got the Electric property which is ignored.
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Cars>
<Name>element_Name</Name>
<Brand>element_Brand</Brand>
<Electric>true</Electric>
<Price>1</Price>
</Cars>
<Cars>
<Name>element_Name</Name>
<Brand>element_Brand</Brand>
<Electric>true</Electric>
<Price>2</Price>
</Cars>
</Root>
How I can ignoring a property of my ObservableCollection on serialization?
Appreciate your help.
Thanks to the comments of #dbc and #Aluan Haddad I found the exact I was finding, this should be my class to achieve that:
public class Car : BindableBase
{
private string _Name;
public string Name
{
get { return _Name; }
set { Set(ref _Name, value); }
}
private string _Brand;
public string Brand
{
get { return _Brand; }
set { Set(ref _Brand, value); }
}
private bool _Electric;
[XmlIgnore] //<----This!
public bool Electric
{
get { return _Electric; }
set { Set(ref _Electric, value); }
}
private double _Price;
public double Price
{
get { return _Price; }
set { Set(ref _Price, value); }
}
}
Is there a standard naming convention for the properties/methods of a node/relationship class when working with Neo4jClient?
I'm following this link Neo4jClient - Retrieving relationship from Cypher query to create my relationship class
However, there are certain properties of my relationship which i can't get any value despite the relationship having it. While debugging my code, i realized certain properties was not retrieved from the relationship when creating the relationship object.
this is my relationship class
public class Creates
{
private string _raw;
private int _sourcePort;
private string _image;
private int _DestinationPort;
private int _eventcode;
private string _name;
private string _src_ip;
private int _src_port;
private string _dvc;
private int _signature_ID;
private string _dest_ip;
private string _computer;
private string _sourceType;
private int _recordID;
private int _processID;
private DateTime _time;
private int _dest_port;
public string Raw { get { return _raw; } set { _raw = value; } }
public int SourcePort { get { return _sourcePort; } set { _sourcePort = value; } }
public string Image { get { return _image; } set { _image = value; } }
public int DestinationPort { get { return _DestinationPort; } set { _DestinationPort = value; } }
public int Eventcode { get { return _eventcode; } set { _eventcode = value; } }
public string Name { get { return _name; } set { _name = value; } }
public string Src_ip { get { return _src_ip; } set { _src_ip = value; } }
public int Src_port { get { return _src_port; } set { _src_port = value; } }
public string DVC { get { return _dvc; } set { _dvc = value; } }
public int Signature_ID { get { return _signature_ID; } set { _signature_ID = value; } }
public string Dest_ip { get { return _dest_ip; } set { _dest_ip = value; } }
public string Computer { get { return _computer; } set { _computer = value; } }
public string SourceType { get { return _sourceType; } set { _sourceType = value; } }
public int RecordID { get { return _recordID; } set { _recordID = value; } }
public int ProcessID { get { return _processID; } set { _processID = value; } }
public DateTime Indextime { get { return _time; } set { _time = value; } }
public int Dest_port { get { return _dest_port; } set { _dest_port = value; } }
}
This is another class
public class ProcessConnectedIP
{
public Neo4jClient.RelationshipInstance<Pivot> bindto { get; set; }
public Neo4jClient.Node<LogEvent> bindip { get; set; }
public Neo4jClient.RelationshipInstance<Pivot> connectto { get; set; }
public Neo4jClient.Node<LogEvent> connectip { get; set; }
}
This is my neo4jclient query to get the relationship object
public IEnumerable<ProcessConnectedIP> GetConnectedIPs(string nodeName)
{
try
{
var result =
this.client.Cypher.Match("(sourceNode:Process{name:{nameParam}})-[b:Bind_IP]->(bind:IP_Address)-[c:Connect_IP]->(connect:IP_Address)")
.WithParam("nameParam", nodeName)
.Where("b.dest_ip = c.dest_ip")
.AndWhere("c.Image=~{imageParam}")
.WithParam("imageParam", $".*" + nodeName + ".*")
.Return((b, bind, c, connect) => new ProcessConnectedIP
{
bindto = b.As<RelationshipInstance<Creates>>(),
bindip = bind.As<Node<LogEvent>>(),
connectto = c.As<RelationshipInstance<Creates>>(),
connectip = connect.As<Node<LogEvent>>()
})
.Results;
return result;
}catch(Exception ex)
{
Console.WriteLine("GetConnectedIPs: Error Msg: " + ex.Message);
return null;
}
}
This is the method to read the results
public void MyMethod(string name)
{
IEnumerable<ProcessConnectedIP> result = clientDAL.GetConnectedIPs(name);
if(result != null)
{
var results = result.ToList();
Console.WriteLine(results.Count());
foreach (ProcessConnectedIP item in results)
{
Console.WriteLine(item.Data.Src_ip);
Console.WriteLine(item.bindto.StartNodeReference.Id);
Console.WriteLine(item.bindto.EndNodeReference.Id);
Console.WriteLine(item.connectto.StartNodeReference.Id);
Console.WriteLine(item.connectto.EndNodeReference.Id);
Node<LogEvent> ans = item.bindip;
LogEvent log = ans.Data;
Console.WriteLine(log.Name);
Node<LogEvent> ans1 = item.connectip;
LogEvent log1 = ans1.Data;
Console.WriteLine(log1.Name);
}
}
}
Somehow, i'm only able to populate the relationship object with src_ip/src_port/dest_ip/dest_port values. the rest are empty.
Is there any possible reason why? I've played with upper/lower cases on the properties names but it does not seem to work.
This is the section of the graph im working with
This is the relationship properties sample:
_raw: Some XML dataSourcePort: 49767Image: C:\Windows\explorer.exeDestinationPort: 443EventCode: 3Name: Bind
IPsrc_ip: 172.10.10.104dvc: COMPUTER-NAMEsrc_port:
49767signature_id: 3dest_ip: 172.10.10.11Computer:
COMPUTRE-NAME_sourcetype:
XmlWinEventLog:Microsoft-Windows-Sysmon/OperationalRecordID:
13405621ProcessId: 7184_time: 2017-08-28T15:15:39+08:00dest_port: 443
I'm not entirely sure how your Creates class is ever populated, in particular those fields - as your Src_port property doesn't match the src_port in the sample you provided (case wise).
I think it's probably best to go back to a super simple version. Neo4jClient will map your properties to the properties in the Relationship as long as they have the same name (and it is case-sensitive).
So start with a new Creates class (and use auto properties - it'll make your life a lot easier!)
public class Creates
{
public string Computer { get; set; }
}
Run your query with that and see if you get a result, then keep on adding properties that match the name and type you expect to get back (int, string etc)
It seems that i have to give neo4j node/relationship property names in lowercase and without special characters at the start of the property name, in order for the above codes to work.
The graph was not created by me at the start thus i had to work on it with what was given. I had to get the developer who created the graph to create the nodes with lowercases in order for the above to work.
I am having an API XML response, that looks like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<artists itemsPerPage="30">
<artist id="a0d9633b">
<url>http://www.somesite.com/3df8daf.html</url>
</artist>
</artists>
For deserializing it I use classes:
[SerializableAttribute]
[XmlTypeAttribute(TypeName = "result")]
public abstract class Result
{
private int _itemsPerPage;
[XmlAttributeAttribute(AttributeName = "itemsPerPage")]
public int ItemsPerPage
{
get { return this._itemsPerPage; }
set { this._itemsPerPage = value; }
}
}
[SerializableAttribute]
[XmlTypeAttribute(TypeName = "artists")]
[XmlRootAttribute(ElementName = "artists")]
public class Artists : Result, IEnumerable<Artist>
{
private List<Artist> _list;
[XmlElementAttribute(ElementName = "artist")]
public List<Artist> List
{
get { return this._list; }
set { this._list = value; }
}
public Artists()
{
_list = new List<Artist>();
}
public void Add(Artist item)
{
_list.Add(item);
}
IEnumerator IEnumerable.GetEnumerator() { return _list.GetEnumerator(); }
public IEnumerator<Artist> GetEnumerator()
{
foreach (Artist artist in _list)
yield return artist;
}
}
[SerializableAttribute]
[XmlTypeAttribute(TypeName = "artist")]
[XmlRootAttribute(ElementName = "artist")]
public class Artist
{
private string _mbid;
private string _url;
[XmlAttributeAttribute(AttributeName = "mbid")]
public string MBID
{
get { return this._mbid; }
set { this._mbid = value; }
}
[XmlElementAttribute(ElementName = "url")]
public string Url
{
get { return this._url; }
set { this._url = value; }
}
public Artist() { }
}
And this is how I do deserialization:
XmlSerializer serializer = new XmlSerializer(typeof(Artists));
Artists result = (Artists)serializer.Deserialize(new StreamReader("artists.xml"));
So, the problem is, that when I do deserialization, ItemsPerPage doesn't have a needed (30) value (it has default 0). However, if I remove IEnumerable<Artist> interface and deserialize Artists, the value will appear. I can remove interface and leaves Artists with only IEnumerator<Artist> GetEnumerator() method, so everything will be fine, Artists still can be foreach'ed, but will no longer be IEnumerable<Artist>. Which is not what I want.
The problem is not in base class, because I can transfer property ItemsPerPage to Artists class, and deserialization result will be the same.
Any ideas how I can deserialize ItemsPerPage value with Artists still being IEnumerable<Artist>?
I'm having a class with only private fields and their public getter-setters. I need to convert class object into JSON String hence I'm using JSON.Net.
Following is a simple snippet to convert class object into a JSON string.
MyClass obj = new MyClass();
string json = JsonConvert.SerializeObject(obj);
Console.WriteLine(json);
But the method SerializeObject throws StackOverflowException at field in MyClass of type DateTime. What's happening here?
Update
Following is how MyClass looks like (as it is, I don't mind sharing the actual class)
class MyClass
{
private int _Model;
public int Model
{
get
{
return _Model;
}
set
{
_Model = value;
}
}
private long _ProductionControlNumber;
public long ProductionControlNumber
{
get
{
return _ProductionControlNumber;
}
set
{
_ProductionControlNumber = value;
}
}
private DateTime _ProductionDate;
public DateTime ProductionDate
{
get
{
return _ProductionDate;
}
set
{
_ProductionDate = value;
}
}
private DateTime _TestDate;
public DateTime TestDate
{
get
{
return _TestDate;
}
set
{
_TestDate = value;
}
}
private DateTime _TestStartTime;
public DateTime TestStartTime
{
get
{
return _TestStartTime;
}
set
{
_TestStartTime = value;
}
}
private TimeSpan _TestDuration;
public TimeSpan TestDuration
{
get
{
return _TestDuration;
}
set
{
_TestDuration = value;
}
}
public DateTime TestEndTime
{
get
{
//TODO Perform start end time computing logic.
return TestEndTime;
}
}
private int _TestBed;
public int TestBed
{
get
{
return _TestBed;
}
set
{
_TestBed = value;
}
}
private long _EngineSerial;
public long EngineSerial
{
get
{
return _EngineSerial;
}
set
{
_EngineSerial = value;
}
}
private Single _FuelSpecificGravity;
public Single FuelSpecificGravity
{
get
{
return _FuelSpecificGravity;
}
set
{
_FuelSpecificGravity = value;
}
}
private long _FuelConsume100;
public long FuelConsume100
{
get
{
return _FuelConsume100;
}
set
{
_FuelConsume100 = value;
}
}
private long _FuelConsume110;
public long FuelConsume110
{
get
{
return _FuelConsume100;
}
set
{
_FuelConsume100 = value;
}
}
private int _TemporaryRPM;
public int TemporaryRPM
{
get
{
return _TemporaryRPM;
}
set
{
_TemporaryRPM = value;
}
}
private int _PermanentRPM;
public int PermanentRPM
{
get
{
return _PermanentRPM;
}
set
{
_PermanentRPM = value;
}
}
private Single _RatedPower;
public Single RatedPower
{
get
{
return _RatedPower;
}
set
{
_RatedPower = value;
}
}
private int _RatedSpeed;
public int RatedSpeed
{
get
{
return _RatedSpeed;
}
set
{
_RatedSpeed = value;
}
}
private double _PulleyDiameter;
public double PulleyDiameter
{
get
{
return _PulleyDiameter;
}
set
{
_PulleyDiameter = value;
}
}
private double _RopeDiameter;
public double RopeDiameter
{
get
{
return _RopeDiameter;
}
set
{
_RopeDiameter = value;
}
}
private Single _FullLoad;
public Single FullLoad
{
get
{
return _FullLoad;
}
set
{
_FullLoad = value;
}
}
}
Also, I'll have another class which will have MyClass type field (along with its own similar set of fields), which is going to be converted into JSON string too, and that shouldn't be a problem since JSON.Net is said to support that situation too.
Note: I'm new to C# but I've previously worked with JSON in Java, where I get to play with JSONObject and JSONArray, and they were pretty straight forward.
It looks like your TestEndTime property's getter references itself. Therefore when Json.NET tries to serialize it, it recursively accesses itself and causes the StackOverflowException.
Hope that helps!
I need to be able to define two nodes with the same name but completely different subnode structures. I didn't design this XML schema but for the time being I'm forced to use it as is. I realize it's a terrible abuse of everything that is XML but there you have it.
What I need it to look like:
<order>
<ItemType type="Clubs">
<Club num="1">
<ClubName>Some Name</ClubName>
<ClubChoice>Something Else</ClubChoice>
</Club>
</ItemType>
<ItemType type="Gift" val="MailGreeting">
<GiftName>MailGreeting</GiftName>
<GiftDescription></GiftDescription>
<GiftQuanity>1</GiftQuanity>
</ItemType
</order>
Of course it's far more complicated than but you get the gist.
I'm using XmlSerializer and would really like to avoid using XDocument but if that's what I need to do then so be it.
If your order contains properties and not a list you can tell the serializer to name the elements like this:
[XmlRoot("order")]
public class Order
{
private Whatever whateverInstance;
[XmlElement("ItemType")]
public Whatever WhateverInstance
{
get { return whateverInstance; }
set { whateverInstance = value; }
}
private Something somethingInstance;
[XmlElement("ItemType")]
public Something SomethingInstance
{
get { return somethingInstance; }
set { somethingInstance = value; }
}
}
If it's a list of things you could get to have a identical element name as well but you will get a redundant xsi:Type attribute:
[XmlRoot("order")]
public class Order
{
private ItemType[] itemTypes;
[XmlElement("ItemType")]
public ItemType[] ItemTypes
{
get { return itemTypes; }
set { itemTypes = value; }
}
}
[XmlInclude(typeof(Clubs))]
[XmlInclude(typeof(Gift))]
public abstract class ItemType
{
private string type = "None";
[XmlAttribute]
public string Type
{
get { return type; }
set { type = value; }
}
}
public class Clubs : ItemType
{
public Clubs()
{
Type = "Clubs";
}
private Club[] clubsArray;
[XmlElement("Club")]
public Club[] ClubsArray
{
get { return clubsArray; }
set { clubsArray = value; }
}
}
public class Club
{
private int num = 0;
[XmlAttribute("num")]
public int Num
{
get { return num; }
set { num = value; }
}
private string clubName = "";
public string ClubName
{
get { return clubName; }
set { clubName = value; }
}
private string clubChoice = "";
public string ClubChoice
{
get { return clubChoice; }
set { clubChoice = value; }
}
}
public class Gift : ItemType
{
public Gift()
{
Type = "Gift";
}
private string val = "";
[XmlAttribute("val")]
public string Val
{
get { return val; }
set { val = value; }
}
private string giftName = "";
public string GiftName
{
get { return giftName; }
set { giftName = value; }
}
private string giftDescription = "";
public string GiftDescription
{
get { return giftDescription; }
set { giftDescription = value; }
}
private int giftQuanity = 0;
public int GiftQuanity
{
get { return giftQuanity; }
set { giftQuanity = value; }
}
}
Test:
List<ItemType> list = new List<ItemType>();
list.Add(new Clubs() { ClubsArray = new Club[] { new Club() { Num = 0, ClubName = "Some Name", ClubChoice = "Something Else" } } });
list.Add(new Gift() { Val = "MailGreeting", GiftName = "MailGreeting", GiftDescription = "GiftDescription", GiftQuanity = 1});
Order order = new Order();
rder.ItemTypes = list.ToArray();
XmlSerializer serializer = new XmlSerializer(typeof(Order));
StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Stuff.xml");
serializer.Serialize(sw, order);
sw.Close();
Output:
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ItemType xsi:type="Clubs" Type="Clubs">
<Club num="0">
<ClubName>Some Name</ClubName>
<ClubChoice>Something Else</ClubChoice>
</Club>
</ItemType>
<ItemType xsi:type="Gift" Type="Gift" val="MailGreeting">
<GiftName>MailGreeting</GiftName>
<GiftDescription>GiftDescription</GiftDescription>
<GiftQuanity>1</GiftQuanity>
</ItemType>
</order>