Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add xml:lang="en" to <html> tag

Tags:

c#

xml

c#-4.0

I have an XElement object for the following xml.

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<body>
<div>Hello world</div>
</body>
</html>

I want to add xml:lang="en" to tag. So it become

<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">

I tried the following code.

XAttribute xmlLang = new XAttribute("xml:lang","en");

But I got the following error:

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

Thanks for your help.

like image 487
CleanCoder Avatar asked Dec 28 '22 03:12

CleanCoder


1 Answers

You need to pass an XName instance that consists of the namespace (http://www.w3.org/1999/xhtml) and the local name (lang) to the XAttribute constructor.

XAttribute xmlLang = new XAttribute(XNamespace.Xml + "lang", "en");
like image 146
dtb Avatar answered Jan 07 '23 12:01

dtb