0

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);
Murtaza Munshi
  • 1,065
  • 4
  • 13
  • 40
  • 2
    show us your code you are using – string.Empty Sep 02 '13 at 09:58
  • Your posted code snippet wouldn't compile. I changed the first line to `XDocument xDoc = ...`, and changed the `varXElement` lines to `var XElement = new XElement("studentNAme", template);` and `xDoc.Element("Mail").Add(varXElement);` and it ran just fine, so something else is going. – Tim Sep 02 '13 at 10:36
  • 1
    I am afraid but i am still not able to run it. It will compile properly no doubt but when u will try to save it giving filepath = "C:\\doc.xml" it will give you the exception of whitespace characters. – Murtaza Munshi Sep 03 '13 at 03:37
  • @MurtazaMunshi - I'm able to save it just fine. However, if you're still having issues, change your save command to this and see if it works: `xDoc.SaveXml(filePath, SaveOptions.DisableFormatting);` and see if that works. – Tim Sep 03 '13 at 04:58

3 Answers3

1

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.

Anand
  • 14,545
  • 8
  • 32
  • 44
1

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

Community
  • 1
  • 1
string.Empty
  • 10,393
  • 4
  • 39
  • 67
1

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.

Tim
  • 28,212
  • 8
  • 63
  • 76