I have an XMl file like this
and a class named parameter with MACId ,PublicIp,LOcalP and Port as property (public string MACId{get; set;})
I have to read this xml and store each value in object of parameter of that class.I have tried it but not able to get the requiered output. so help me to solve this issue.
VulpesPosted Sep 22, 2014, 2:48 PM
using System;
using System.Xml.Linq;
public class Parameter
{
public string MACId {get; set;}
public string LocalIp {get; set;}
public string PublicIp {get; set;}
public string Port {get; set;}
public override string ToString()
{
return String.Format("MAC ID = {0}\nLocal IP = {1}\nPublic IP = {2}\nPort = {3}", MACId, LocalIp, PublicIp, Port);
}
}
class Program
{
static void Main()
{
Parameter p = new Parameter();
XElement reg = XElement.Load("amrendra.xml");
p.MACId = reg.Element("MAC_ID").Attribute("V").Value;
p.LocalIp = reg.Element("LOCAL_IP").Attribute("V").Value;
p.PublicIp = reg.Element("PUBLIC_IP").Attribute("V").Value;
p.Port = reg.Element("PORT").Attribute("V").Value;
Console.WriteLine(p);
}
The output is:
MAC ID = 123qwwe
Local IP = 192.70.72.20
Public IP = 192.80.70.68
Port = 1233
Wim SturkenboomPosted Sep 22, 2014, 4:18 PM
Wim SturkenboomPosted Sep 22, 2014, 3:23 PM
In VS2012 and newer
create a new class file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfigWithAttributes
{
class Class1
{
}
}
Remove the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfigWithAttributes
{
}
Copy the content of your XML to the clipboard and place your cursor in the empty line in the namespace.
From the visualstudio menu, choose edit -> paste special -> paste xml ass classes
That will give you the complete structure
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfigWithAttributes
{
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class REGISTRATION
{
private REGISTRATIONMAC_ID mAC_IDField;
private REGISTRATIONLOCAL_IP lOCAL_IPField;
private REGISTRATIONPUBLIC_IP pUBLIC_IPField;
private REGISTRATIONPORT pORTField;
///
public REGISTRATIONMAC_ID MAC_ID
{
get
{
return this.mAC_IDField;
}
set
{
this.mAC_IDField = value;
}
}
///
public REGISTRATIONLOCAL_IP LOCAL_IP
{
get
{
return this.lOCAL_IPField;
}
set
{
this.lOCAL_IPField = value;
}
}
///
public REGISTRATIONPUBLIC_IP PUBLIC_IP
{
get
{
return this.pUBLIC_IPField;
}
set
{
this.pUBLIC_IPField = value;
}
}
///
public REGISTRATIONPORT PORT
{
get
{
return this.pORTField;
}
set
{
this.pORTField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class REGISTRATIONMAC_ID
{
private string vField;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public string V
{
get
{
return this.vField;
}
set
{
this.vField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class REGISTRATIONLOCAL_IP
{
private string vField;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public string V
{
get
{
return this.vField;
}
set
{
this.vField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class REGISTRATIONPUBLIC_IP
{
private string vField;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public string V
{
get
{
return this.vField;
}
set
{
this.vField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class REGISTRATIONPORT
{
private ushort vField;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public ushort V
{
get
{
return this.vField;
}
set
{
this.vField = value;
}
}
}
}
Next add a deserializer to the first class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfigWithAttributes
{
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class REGISTRATION
{
private REGISTRATIONMAC_ID mAC_IDField;
private REGISTRATIONLOCAL_IP lOCAL_IPField;
private REGISTRATIONPUBLIC_IP pUBLIC_IPField;
private REGISTRATIONPORT pORTField;
///
/// read a configuration file
///
/// file to read
/// possible error message
///
public static REGISTRATION DeserializeFromXML(string strFilename, ref string errmsg)
{
System.IO.TextReader textReader;
errmsg = "";
// open file
try
{
textReader = new System.IO.StreamReader(strFilename);
}
catch (Exception ex)
{
errmsg = ex.Message;
if (ex.InnerException != null && ex.InnerException.Message != null)
errmsg += "\t" + ex.InnerException.Message;
return null;
}
// deserialize from xml
REGISTRATION myRegistration;
try
{
myRegistration = new REGISTRATION();
System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(myRegistration.GetType());
myRegistration = (REGISTRATION)deserializer.Deserialize(textReader);
textReader.Close();
}
catch (Exception ex)
{
errmsg = ex.Message;
if (ex.InnerException != null && ex.InnerException.Message != null)
errmsg += "\t" + ex.InnerException.Message;
textReader.Close();
return null;
}
return myRegistration;
} // end_of_DeserializeFromXML(string filename, ref string errmsg)
///
public REGISTRATIONMAC_ID MAC_ID
{
get
{
return this.mAC_IDField;
}
set
{
this.mAC_IDField = value;
}
}
///
public REGISTRATIONLOCAL_IP LOCAL_IP
{
get
{
return this.lOCAL_IPField;
}
set
{
this.lOCAL_IPField = value;
}
}
///
public REGISTRATIONPUBLIC_IP PUBLIC_IP
{
get
{
return this.pUBLIC_IPField;
}
set
{
this.pUBLIC_IPField = value;
}
}
///
public REGISTRATIONPORT PORT
{
get
{
return this.pORTField;
}
set
{
this.pORTField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class REGISTRATIONMAC_ID
{
private string vField;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public string V
{
get
{
return this.vField;
}
set
{
this.vField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class REGISTRATIONLOCAL_IP
{
private string vField;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public string V
{
get
{
return this.vField;
}
set
{
this.vField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class REGISTRATIONPUBLIC_IP
{
private string vField;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public string V
{
get
{
return this.vField;
}
set
{
this.vField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class REGISTRATIONPORT
{
private ushort vField;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public ushort V
{
get
{
return this.vField;
}
set
{
this.vField = value;
}
}
}
}
You can use it as follows:
static void Main(string[] args)
{
string errmsg = "";
REGISTRATION reg = REGISTRATION.DeserializeFromXML("test.xml", ref errmsg);
if (reg == null)
{
// error
}
else
{
Console.WriteLine(reg.LOCAL_IP.V);
}
Console.ReadLine();
}