Error in loading xml file in XmlDocument

前端 未结 3 1839
感情败类
感情败类 2021-01-21 19:22

Hi I have below xml file that I am trying to load in xml document using below code -

XmlDocument Doc = new XmlDocument();
Doc.LoadXml(@\"C:\\MappingFiles\\Input         


        
相关标签:
3条回答
  • 2021-01-21 20:12

    Use XMLDocument.Load(). This accepts an xml file

    whereas XMLDocument.LoadXml() accepts an xml string.

    0 讨论(0)
  • 2021-01-21 20:18

    If you really want to read it in as a string and don't want to do XMLDocument.Load() as others have suggested, you can do XMLDocument.LoadXml(), but it should be as a string, first:

    string myFilePath = @"C:\MappingFiles\InputFile.xml";
    string allText = File.ReadAllText(myFilePath);
    
    XmlDocument xmlDoc = new XmlDocument();
    try
    {
        xmlDoc.LoadXml(allText);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }
    

    I found that this can work even when .Load() on the file, itself, does not.

    0 讨论(0)
  • 2021-01-21 20:20

    Your calling the wrong method, see this question and answer.

    You should be calling XmlDocument.Load.


    In fact, you'd be better off doing

    var doc = XDocument.Load("path");
    

    and using linq to xml.

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