version : protobuf-net r282
Serialize XElement object and deserialize it will result lost of relationship information such like NextNode, Parent... It looks like that only Xml data in it is stored.
Is there any way to store relationship information?
Thanks!
Here is a class I have used to test:
[ProtoContract]
public class Person
{
[ProtoMember(1)]
public string FirstName { get; set; }
[ProtoMember(2)]
public string FamilyName { get; set; }
[ProtoMember(3)]
public int Age { get; set; }
[ProtoMember(4)]
public XDocument Details { get; set; }
[ProtoMember(5)]
public XElement Business { get; set; }
// ctor
public Person() { } // ctor for Deserialize
public Person(string first, string family, int age, XDocument details)
{
FirstName = first;
FamilyName = family;
Age = age;
Details = details;
Business = Details == null ? null : Details.Descendants("business").FirstOrDefault();
}
// calculated properties
public string FullName { get { return FirstName + " " + FamilyName; } }
// Methods
public string GetDetails(string key)
{
if (this.Details == null) return null;
var found = (from n in Details.Descendants(key)
select n.Value).FirstOrDefault();
return found;
}
}
[Update]
One way to avoid the problem is serialize absolute path of xelement instead of itself. Here is a sample.
using System.Xml.XPath;
.....
//[ProtoMember(5)]
public XElement Business { get; set; }
[ProtoMember(5)]
public string BusinessSerialized
{
get { return Business == null ? null : Business.GetAbsoluteXPath(); }
set
{
if (value == null) { Business = null; }
else
{
Business = Details.XPathSelectElements(value).FirstOrDefault();
}
}
}
GetAbsoluteXPath is a extension method for XElement. I have found it in this question.