Reputation: 369
i need to add a new Element to a specific part of xml tree but i can't make it work.
this is my xml input
<structMap LABEL="Logical Structure" TYPE="LOGICAL">
<div ID="DIVL1" TYPE="CONTENT">
<div ID="DIVL2" TYPE="" DMDID="MODSMD_ARTICLE1" LABEL="">
<div ID="DIVL3">
<div ID="DIVL31" TYPE="TITLE" />
</div>
</div>
</div>
</structMap>
and here is my desired output
<structMap LABEL="Logical Structure" TYPE="LOGICAL">
<div ID="DIVL1" TYPE="CONTENT">
<div ID="DIVL2" TYPE="" DMDID="MODSMD_ARTICLE1" LABEL="">
<div ID="DIVL3">
<div ID="DIVL31" TYPE="TITLE">
<fptr>
<area BETYPE="IDREF" FILEID="ALTO0011" BEGIN="P11_TB3"/>
</fptr>
</div>
</div>
</div>
</div>
</structMap>
and here is my code
var b = dc.Descendants().Attributes("TYPE").Where(ee => ee.Value == "TITLE").First();
i don't have b.AddFist. how can i make it work?
Upvotes: 0
Views: 78
Reputation: 34421
Usse xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement title = doc
.Descendants("div")
.Where(x => (string)x.Attribute("TYPE") == "TITLE")
.FirstOrDefault();
title.Add(new XElement("fptr", new object[] {
new XElement("area", new object[] {
new XAttribute("BETYPE","IDREF"),
new XAttribute("FILEID","ALTO0011"),
new XAttribute("BEGIN","P11_TB3")
})
}));
}
}
}
Upvotes: 1
Reputation: 5132
Assuming, of course, that you're using LINQ to XML, it's not surprising that you wouldn't have b.AddFirst()
. b
is an XAttribute, not an XElement.
It looks like you're looking for b.Parent.AddFirst()
.
Upvotes: 1