I have a following XML structure :
<html xmlns ="http://www.w3.org/1999/xhtml" >
<head>
. .
</head>
<body>
<div id="1"></div>
<div id="2"></div>
</body>
</html>
I used linq to xml to access div with id = “2”. I loaded document in XDocument :
XDocument ndoc = XDcoument.load(path);
XElement n = new XElement("name","value");
XNamespace xn = "http://www.w3.org/1999/xhtml";
ndoc.Descendants(xn + "div").Single(p => p.Attribute("id").Value == "1").Add(n);
OR
ndoc.Descendants("div").Single(p => p.Attribute("id").Value == "1").Add(n);
I tried out both case in each case there was an exception sequence contain no element. Whats the problem here?
You can give this Xml Library a whirl and it should get it easily with: