d hhdohd after changing att...">

Change xml attribute name in

How to change XElement attribute name in C #?

So if

<text align="center"> d hhdohd </text> 

after changing attribute name align to text-align

 <text text-align="center> d hhdohd </text> 
+4
source share
4 answers

Using LINQ-XML , you can remove the existing attribute, and then add a new one.

Xml markup:

 <?xml version="1.0" encoding="utf-8"?> <root> <text align="center" other="attribute">Something</text> </root> 

code:

  XDocument doc = XDocument.Load(file); var element = doc.Root.Element("text"); var attList = element.Attributes().ToList(); var oldAtt = attList.Where(p => p.Name == "align").SingleOrDefault(); if (oldAtt != null) { XAttribute newAtt = new XAttribute("text-align", oldAtt.Value); attList.Add(newAtt); attList.Remove(oldAtt); element.ReplaceAttributes(attList); doc.Save(file); } 
+5
source

I think you will need to remove and add again, not a sure syntax from my head. but you must be able to xpath for node. Grab the value of an existing attribute, delete the attribute, create a new attribute and assign it the old value.

0
source

Using , you can use the XElement.ReplaceAttributes xml attribute update method, something like this:

 XElement data = XElement.Parse (@"<text align=""center""> d hhdohd </text>"); data.ReplaceAttributes( new XAttribute("text-align", "center") ); 
0
source

use XmlElement SetAttribute and RemoveAttribute

0
source

All Articles