I am parsing an XML document and saving it's content to a simple database via Microsoft Lightswitch application. The XML document contains some user accounts and has the following tags:
<Data Account="i:0#.w|ab001\cn0ied32" Access="0" />
<Data Account="i:0#.w|ab001\cn0ied50" Access="0" />
<Data Account="i:0#.w|ab001\cn0ied56" Access="0" />
<Data Account="i:0#.w|ab001\cn0iit01" Access="0" />
<Data Account="i:0#.w|ab001\cn0iit05" Access="0" />
I want to save the Account without the i:0#.w| before the domain and user name. So it should result in just domain\name after processing.
Here is the my class:
public class MessageInterpreter
{
[XmlRoot("Root", IsNullable = false)]
[Serializable]
public class Root
{
[XmlElement("Data")]
public Data[] Data{ get; set; }
[Serializable]
public class Data
{
public Data()
{
//Access = false;
}
[XmlIgnore]
public String Account_Nullable { get; set; }
[XmlAttribute]
public String Account { get { return Account.Replace("i:0#.w|", String.Empty); } set { Account_Nullable = value; } }
[XmlIgnore]
public bool? Access_Nullable { get; set; }
[XmlAttribute]
public bool Access { get { return Access_Nullable.GetValueOrDefault(); } set { Access_Nullable = value; } }
}
}
private static XmlSerializer _Serializer;
static MessageInterpreter()
{
_Serializer = new XmlSerializer(typeof(Root));
}
public MessageInterpreter()
{
}
public Root ParseMessage(String message)
{
if (String.IsNullOrEmpty(message))
return null;
using (var stream = new StringReader(message))
{
try
{
return (Root)_Serializer.Deserialize(stream);
}
catch (Exception)
{
return null;
}
}
}
Now when I try to import it, I get a Stack overflow exception ... I am not sure why though. Any ideas how to make it work? Or recommendations of what to/not to use?
Look at your Account
property:
public String Account { get { return Account.Replace("i:0#.w|", String.Empty); }
That's recursive... hence the stack overflow. Given your setter, I suspect you meant
public String Account { get { return Account_Nullable.Replace("i:0#.w|", String.Empty); }
Admittedly that's probably not what I'd do - I'd load/save the data in a fairly transparent way, and then explicitly modify the Account
values appropriately. But hey...
See more on this question at Stackoverflow