0

I have an xml file with a node that contains some c# code.

<Script Name="WrapText">
        var sb = new System.Text.StringBuilder();
        int lastSpaceIndex = 0;

            for(int i = 0; i < paragraph.length; i++)
            {
                var curChar = paragraph[i];
                sb.Append(curChar);

                if (System.Char.IsWhiteSpace(curChar))
                {
                    lastSpaceIndex = i;
                }

                if (i % splitlength == 0)
                {
                    if (lastSpaceIndex != 0)
                    {
                        sb[lastSpaceIndex] = '\n';
                    }
                }
            }

        return sb.ToString();
</Script>

when i try to load this using an XmlDocument and XmlReader classes in C# like this:

    XmlReader xReader = XmlReader.Create(new MemoryStream(ASCIIEncoding.UTF8.GetBytes(imml)),  _ReaderSettings);
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(xReader);

I get this error:

    Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 25, position 21.

If I remove the for loop line, it loads the file without any problems.

Why is this happening?

1
  • if you change your xml file to have CDATA around the c# code, does it work? Commented Jan 27, 2012 at 2:09

1 Answer 1

3

Creation of XML by hand/with string concatenation always causes such problems: you < is not properly encoded and as result < paragraph.length; i++)... is treated as element name.

You should encode all special characters as required. (i.e. &lt; for <). See http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references for compact list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.