i am trying to get a xml from classes that i defined. This are my classes
public class MyClass
{
public string Name { get; set; }
public MyAttribute[] Elements { get; set; }
}
public class MyAttribute
{
public string Name { get; set; }
public object Value { get; set; }
public string Type { get; private set; }
}
MyClass myClass = new MyClass();
myClass.Name = "Class1";
myClass.Elements = new MyAttribute[3] {
new MyAttribute(){ Name = "Att1", Value = 4 },
new MyAttribute(){ Name = "Att2", Value = 5 },
new MyAttribute(){ Name = "Att3", Value = 6 }
};
i would like get this xml
<?xml version="1.0" encoding="utf-8" ?>
<Class1>
<Att1>4</Att1>
<Att2>5</Att2>
<Att3>6</Att3>
</Class1>
is possible generate this xml and his xsd. thanks.
EDIT : I solved using XmlDocument class (System.Xml) like this:
public class MyClass
{
public string Name { get; set; }
public MyAttribute[] Elements { get; set; }
public XmlDocument Xml()
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement(this.Name);
foreach (MyAttribute att in this.Elements)
{
XmlElement xmlElement = xmlDoc.CreateElement(att.Name);
xmlElement.InnerText = att.Value.ToString();
rootNode.AppendChild(xmlElement);
}
xmlDoc.AppendChild(rootNode);
return xmlDoc;
}
}
For XSD, I'm using XmlSchema (System.Xml.Schema)