Adding attributes in LINQ using element.add
I have to write information from textboxes to XML file on click event. My sample code would look lik this.
XDocument xmlDoc = XDocument.Load(fileName);
xmlDoc.Element("Mediaplans").Add(new XElement("MediaPlan", 开发者_运维知识库new XElement("Media",TxtMedia.Text),
new XElement("Adtype", TxtAdtype.Text), new XElement("EmailId",TxtEmailId.Text)));
xmlDoc.Save(fileName).
What I want to know is how do we add attributes to elements with the above method? I am new to this field. any help appreciated.
Thanks, Kruthika
You can call Add
and pass an XAttribute
too.
You can just use add new XAttribute like you have done with XElement.
Have a look at this link for an example
I believe that you should be able to do something like
XDocument xmlDoc = XDocument.Load(fileName);
xmlDoc.Element("Mediaplans").Add(new XAttribute("File name", fileName),new XElement("MediaPlan", new XElement("Media",TxtMedia.Text), new XElement("Adtype", TxtAdtype.Text), new XElement("EmailId",TxtEmailId.Text));
xmlDoc.Save(fileName).
Sorry dont have access to VS at the moment so I cant verify the code.
精彩评论