Reputation: 1085
I am creating an Xml XDocument. When i try to insert a string which contains alphabets, whitespaces, special Characters and numbers. It gives an error at runtime.
The ' ' character, hexadecimal value 0x20, cannot be included in a name.
How can i insert such type of string. Is there any other way to insert this type of string.
Code i am Using:
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("Mail"));
var template = @"To MOM
13, AD1
tr y
fghdh, Madhya Pradesh, India
Dear Ram,
We would like to appoint you for new job";
XElement varXElement = new XElement("studentName", template);
xDoc.Element("Mail").Add(varXElement);
xDoc.Save(filePath);
Upvotes: 0
Views: 1561
Reputation: 28530
I'm not sure why it's not working for you - the following code snippet works for me (need to run VS as administrator to save to the root of C:):
using System;
using System.Xml.Linq;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("Mail"));
var template = @"To MOM
13, AD1
tr y
fghdh, Madhya Pradesh, India
Dear Ram,
We would like to appoint you for new job";
XElement varXElement = new XElement("studentName", template);
xDoc.Element("Mail").Add(varXElement);
xDoc.Save("C:\\doc.xml");
Console.ReadLine();
}
}
}
This produces the following XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Mail>
<studentName>To MOM
13, AD1
tr y
fghdh, Madhya Pradesh, India
Dear Ram,
We would like to appoint you for new job</studentName>
</Mail>
This was with VS 2012 Premium on a Win7 64-bit machine.
Upvotes: 1
Reputation: 10552
The ' ' character, hexadecimal value 0x20, cannot be included in a name.
As the error states you may not put a space in a xml element name.
more info can be found here:
White space in XmlElement in C# .Net
Can I create an element with forward slash as part of the name
Upvotes: 1
Reputation: 14915
If you are trying to do something like this
<some text some>ABAC</some text some>
Then its illegal in xml grammar. If you want to achieve something like this then you have to use attributes
<node name ="some text some">ABAC</node >
But it's very hard to guess from your question what is your problem.
Upvotes: 1