Search code examples
c#xmlexceptionwebconfigurationmanager

Exception with WebConfigurationManager.AppSettings.Get("urlAddress")


I am very new to working with XML. I've been weeding my way through debugging a project that was handed to me, but have run into quite a wall.

My code:

XmlWriter xmlWriter = XmlWriter.Create("ToPost.xml");

    xmlWriter.WriteStartDocument();
    xmlWriter.WriteStartElement("eclRequest xmlns='" + WebConfigurationManager.AppSettings.Get("urlAddress") + "'");
    .....

But the WebConfigurationManager.AppSettings.Get("urlAddress") is giving me the following Exception:

Invalid name character in 'eclRequest xmlns='''. The ' ' character, hexadecimal value 0x20, cannot be included in a name.

I was wondering what exactly this Get() statement is accessing, and what could be causing the Exception?


Solution

  • This is not exception of AppSettings.Get but of xmlWriter.WriteStartElement, which has signature:

    public void WriteStartElement(
        string localName
    )
    

    and hence, localName is just name of start XML tag, cannot contain space (' '). But there are other overloads, you are probably interested in:

    public void WriteStartElement(
        string localName,
        string ns
    )
    

    where ns:

    The namespace URI to associate with the element. If this namespace is already in scope and has an associated prefix, the writer automatically writes that prefix also.