14

I am trying to grab a schema and validate against my xml.

  XmlReaderSetting settings = new System.Xml.XmlReaderSettings();
            settings.Schemas.Add(null, "http://example.com/myschema.xsd");
            settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler); 
            settings.ValidationType = ValidationType.Schema;
            settings.IgnoreWhitespace = false;
            XmlReader reader = XmlReader.Create(xml, settings);

I get

Invalid URI: The Uri string is too long


System.UriFormatException was unhandled   Message=Invalid URI: The Uri string is too long.   Source=System   StackTrace:
       at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
       at System.Uri..ctor(String uriString, UriKind uriKind)
       at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri)
       at System.Xml.XmlUrlResolver.ResolveUri(Uri baseUri, String relativeUri)
       at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
       at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
       at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings)
       at ConsoleApplication2.Program.Main(String[] args) in Program.cs:line 42
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()   InnerException:

Does not tell me what the max length is or anything. Anyone ever get this before?

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • which line is the crash occurring at? wht is xml here referring to? – Baz1nga Aug 12 '11 at 17:03
  • 2
    http://stackoverflow.com/q/417142/684934 seems to suggest you're going to be limited to 2000 characters or so. –  Aug 12 '11 at 16:56
  • xml is a string that holds the xml that needs to be validated. It crashes on the XmlReader.Create() – chobo2 Aug 12 '11 at 17:07
  • 5
    For anyone else, I got similar error when I had an xmlDoc.Load instead of xmlDoc.LoadXml. It saw the xml contents as a filename/URI. – NealWalters Sep 20 '12 at 20:40

1 Answers1

11

the problem is that the xml in your xmlreader.create function should be a uri if the argument is a string.

eg.

XmlReader reader = XmlReader.Create("http://ServerName/data/books.xml", settings);

In your case the xml file is being interpreted as the url and hence it is complaining about the limit.

look at this msdn doc XmlReader.Create Method for different overloaded methods..

I am guessing you should use the TextReader one.

Yann Duran
  • 3,842
  • 1
  • 24
  • 24
Baz1nga
  • 15,485
  • 3
  • 35
  • 61