我有一个具有类属性的类:
[XmlRoot(ElementName = "RootXML")]
public class Apply
{
/My Properties
}
现在从上面的类创建一个xml,我使用下面的函数:
public virtual string RenderXml()
{
XmlTextWriter writer = null;
try
{
MemoryStream ms = new MemoryStream();
writer = new XmlTextWriter(ms, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
_xmlSerializer.Serialize(writer, this, ns);
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
return sr.ReadToEnd();
}
}
finally
{
if (writer != null)
writer.Close();
}
}
我的问题是如何将属性添加到“RootXML”并从配置文件和函数中读取属性值,例如
<RootXML attr1="read from config" attr2="read from function" >
<Property1>value</Property1>
</RootXML>