XmlDocument.Load Vs XmlDocument.LoadXml

前端 未结 5 1408
面向向阳花
面向向阳花 2021-01-07 18:41

I just came across with a problem using XmlDocument.LoadXml.

The application was crashing, giving the following error:

\"Data at

相关标签:
5条回答
  • 2021-01-07 19:16

    Assuming your using XmlDocument.Load and XmlDocument.LoadXml in the right way this problem may be caused by Byte Order Mark.

    This other question might be useful.

    0 讨论(0)
  • 2021-01-07 19:17

    XmlDocument.Load is used to load XML either from a stream, TextReader, path/URL, or XmlReader. XmlDocument.LoadXml is used to load the XML contained within a string.

    They're fundamentally different ways of loading XML, depending on where the XML is actually stored. So it sounds like you were using the wrong method for where your XML is.

    0 讨论(0)
  • 2021-01-07 19:21

    Were you trying to use XmlDocument.LoadXml and passing in the name of a file? It doesn't do that - it assumes that the string you pass in is the XML. So you might use:

    doc.LoadXml("<root><child /><root>");
    

    or

    doc.Load("myfile.xml");
    

    If that doesn't help, could you show your failing and working code? There are different ways you could have changed from using LoadXml to Load...

    0 讨论(0)
  • 2021-01-07 19:26

    The application was crashing with the following error: "Data at the root level is invalid. Line 1, position 1" I suspect you xml data does not have a root level: for example:

    <area id="1">
      <candidate id="0">dataata</candidate>
    </area>
    <area id="2">
      <candidate id="0">dataataa</candidate>
    </area>
    

    you need have at least one root level on top of the bottom levels. for example:

    <areas>
      <area id="1">
        <candidate id="0">dataata</candidate>
      </area>
      <area id="2">
        <candidate id="0">dataataa</candidate>
      </area>
    </areas>
    

    so please put one mother on the top of your level, make it grand grand mother of all children

    0 讨论(0)
  • 2021-01-07 19:30

    Load() loads from a certain source, whereas LoadXml() loads directly from a string

    0 讨论(0)
提交回复
热议问题