3

I have a class with the class attribute :

[XmlRoot(ElementName = "RootXML")]
public class Apply
{
    /My Properties
}

now to create an xml from the above class I use below function :

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();
    }
}

My question is how do i add attributes to "RootXML" and read the value of attribute from config file and from function e.g.

<RootXML attr1="read from config" attr2="read from function" >
    <Property1>value</Property1>
</RootXML>
djv
  • 15,168
  • 7
  • 48
  • 72
Zaki
  • 5,540
  • 7
  • 54
  • 91
  • Did you try this :`writer.WriteAttributeString("AttrNAme", "AttrValue");` – Saravanan May 24 '13 at 10:39
  • tried this : writer.WriteAttributeString("RootXML", "attr1=" + System.Configuration.ConfigurationManager.AppSettings["id"]); but get exception : threw an exception of type 'System.InvalidOperationException – Zaki May 24 '13 at 10:44
  • what was the stack trace. – Saravanan May 24 '13 at 10:45
  • at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces) at PDB.UK.UI.ApplyInfoContainer.RenderXml() in C:\Proj\Container.cs:line 227 at btnSubmit_Click(Object sender, EventArgs e) in C:\Proj\Project\Create.aspx.cs:line 185 – Zaki May 24 '13 at 10:47

1 Answers1

3

You can add to your class property attribute [XmlAttribute] and that property will be serilized as attribute

[XmlRoot(ElementName = "RootXML")]
public class Apply
{
    private string _testAttr="dfdsf";



    [XmlAttribute]
    public String TestAttr
    {
        get { return _testAttr; }

        set { _testAttr = value; }
    }
}

Serialization result for that class

<RootXML TestAttr="dfdsf" />

Added for last comment. If i understand correctly you need to have only one key in session. If it true, that you can use something like that:

string GetKey(){

      if (String.IsNullOrEmpty(HttpContext.Current.Session["mySessionKey"].ToString()))
                HttpContext.Current.Session["mySessionKey"] = GenereteKey();
      return HttpContext.Current.Session["mySessionKey"].ToString();

}
Frank59
  • 3,141
  • 4
  • 31
  • 53
  • It works, but got a question.I called a static function like so : private string _testAttr= GetKey(); which will generate a guid and put it in HttpContext.Current.Session...my question is, would this create one key per session? – Zaki May 24 '13 at 11:09
  • @Sam,see answer fix. This is what you need? – Frank59 May 24 '13 at 11:19