开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜