Click here to Skip to main content
15,943,231 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All

I was un able to execute a simple code shown below
Please help on the same
C#
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                    "<title>Pride And Prejudice</title>" +
                    "</book>");

    //Create an attribute.
    XmlAttribute attr = doc.CreateAttribute("publisher#");
    attr.Value = "WorldWide Publishing";

    //Add the new node to the document.
    doc.DocumentElement.SetAttributeNode(attr);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Posted
v2
Comments
[no name] 3-Apr-13 8:14am    
Why is it that you are unable to run the code?
[no name] 3-Apr-13 8:16am    
Ah, I see... you need to read the documentation. http://msdn.microsoft.com/en-us/library/875kz807.aspx
Ron.bharath 3-Apr-13 8:22am    
the xml attribute is not allowed to have special characters. i want to escape that special character.

1 solution

The line,
XmlAttribute attr = doc.CreateAttribute("publisher#");

causes the issue.

Special character '#' is not valid for attribute name.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900