受欢迎的博客标签

error message:ArgumentException: '', hexadecimal value 0x1B, is an invalid character. in asp .net core

Published

.NET Core version:ASP.NET Core 2.x

OS:windows server 2012 R2

     

I am getting an error on my website with non-ASCII characters ending up in an http header. My website has URLs with non-ASCII characters.         my  code in www.iaspnetcore.com:

An unhandled exception occurred while processing the request.
ArgumentException: '', hexadecimal value 0x1B, is an invalid character.
System.Xml.XmlEncodedRawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd)
ArgumentException: '', hexadecimal value 0x1B, is an invalid character.
System.Xml.XmlEncodedRawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd)
System.Xml.XmlEncodedRawTextWriter.WriteString(string text)
System.Xml.XmlEncodedRawTextWriterIndent.WriteString(string text)
System.Xml.XmlWellFormedWriter.WriteString(string text)
System.Xml.Linq.ElementWriter.WriteElement(XElement e)
System.Xml.Linq.XElement.WriteTo(XmlWriter writer)
System.Xml.Linq.XContainer.WriteContentTo(XmlWriter writer)
System.Xml.Linq.XNode.GetXmlString(SaveOptions o)
System.Xml.Linq.XNode.ToString()
Nop.Web.Framework.Mvc.Rss.RssFeed.GetContent() in RssFeed.cs
+
            return XmlHelper.XmlDecode(document.ToString());
Nop.Web.Framework.Mvc.RssActionResult.ExecuteResultAsync(ActionContext context) in RssActionResult.cs
+
            Content = Feed.GetContent();

 

Dealing with invalid XML hexadecimal characters

error messge

System.Xml.XmlEncodedRawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd)
System.Xml.XmlEncodedRawTextWriter.WriteString(string text)
System.Xml.XmlEncodedRawTextWriterIndent.WriteString(string text)
System.Xml.XmlWellFormedWriter.WriteString(string text)
System.Xml.Linq.ElementWriter.WriteElement(XElement e)
System.Xml.Linq.XElement.WriteTo(XmlWriter writer)
System.Xml.Linq.XContainer.WriteContentTo(XmlWriter writer)
System.Xml.Linq.XNode.GetXmlString(SaveOptions o)
System.Xml.Linq.XNode.ToString()
Nop.Web.Framework.Mvc.Rss.RssFeed.GetContent() in RssFeed.cs
+
            var xmlstring = document.ToString();
Nop.Web.Framework.Mvc.RssActionResult.ExecuteResultAsync(ActionContext context) in RssActionResult.cs
+
            Content = Feed.GetContent();

 

Solution 1.set the XmlReaderSettings.CheckCharacters property to false

You can use the XmlReader and set the XmlReaderSettings.CheckCharacters property to false. This will let you to read the XML file despite the invalid characters. From there you can import pass it to a XmlDocument or XDocument object.

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings { Encoding = Encoding.UTF8, CheckCharacters = false };

Solution 2:

 

Resource

XML Exception: Invalid Character(s)

https://blog.csdn.net/lanmao100/article/details/5216097