none
Difficulty in migrating from MSXML2 to System.xml RRS feed

  • Question

  • Hi,

    I am new to both MSXML as well as System.xml. The below piece of code needs to be changed to the corresponding code by using System.xml. I went through several articles but was unable to find a solution. I am also not sure what exactly the below code does. Please help. Thank you.

    MSXML2.DOMDocument40 xmlDoc;
    bool bXMLString = true;
    string strXMLFile = "<path of xml file>";
    string result = string.Empty;
    
    // Load DOM Document.
    xmlDoc = new MSXML2.DOMDocument40();
    xmlDoc.async = false;
    xmlDoc.load(strXMLFile);
    xmlDoc.validateOnParse = true;
    xmlDoc.resolveExternals = true;
    
    
    // Parse the XML.
    if (xmlDoc.readyState == 4)
    {
        if (xmlDoc.parseError.errorCode != 0)
        {
             if (xmlDoc.parseError.errorCode == -1072896682)
                 bXMLString = false;
             
             result = xmlDoc.parseError.reason + xmlDoc.parseError.srcText;
         }
         
         else
             result = "";
    }
                

    Wednesday, December 28, 2016 6:48 AM

All replies

  • Somthing similar to this:

    bool bXMLString = false;	// better set it to true when all completed successfully instead
    string strXMLFile = "<path of xml file>";
    string result = string.Empty;
    
    // Since 4.5.2 or later defaults XmlReaderSettings.XmlResolver to null, it's recommanded to create it yourself if your reader needs it
    // if you already know what external URL your XML document will be referencing, consider using XmlSecureResolver
    XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = CredentialCache.DefaultCredentials;
    
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.Async = false;
    settings.XmlResolver = resolver;
    // since XmlReader always vaildate XML content, no need for a corresponding setting here.
    try
    {
    	using (XmlReader reader = XmlReader.Create(strXMLFile))
    	{
    		reader.MoveToContent();
    		while (reader.Read())
    		{
    			//...
    		}
    	}
    	bXMLString = true;
    }
    catch (XmlException xex)
    {
    	result = String.Format("XML parser error on line {0} col {1}: {2}", xex.LineNumber, xex.Position, xex.Message);	
    }

    Note that if you want to show the srcText, you may actually need to open a normal StreamReader and call ReadLine() until jumping to corresponding line number to fetch it.



    • Edited by cheong00Editor Wednesday, December 28, 2016 8:40 AM
    • Proposed as answer by Kristin Xie Thursday, December 29, 2016 7:08 AM
    Wednesday, December 28, 2016 8:39 AM
    Answerer
  • Hi Ashfaque Anwar,

    I agree with cheong. He already answered most of this below (use XmlReader or XmlDocument to produce the XML).

    Here I also recommend you have a look at this good article.

    This step-by-step article describes the features of MSXML   and .NET Framework Classes for XML processing. This article also includes   samples on how to use MSXML in Microsoft Visual Basic 6.0 and how to use .NET   Framework Classes for XML processing.

    In .Net, The System.Xml namespace provides standards-based support for processing XML. System.xml is not just the managed version of MSXML; its functionality may overlap that of the MSXML COM library, and it also contains a rich object model and hierarchy.

    For more details, please refer to HOW TO: Implement Common MSXML Tasks in System.xml By Using Visual C# .NET

    Here is a MSDN link for  A Beginner's Guide to the XML DOM

    https://msdn.microsoft.com/en-us/library/aa468547.aspx

    Hope it helps!

    Have a nice day!
    Kristin



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Kristin Xie Thursday, December 29, 2016 7:09 AM
    Thursday, December 29, 2016 7:08 AM
  • Thanks for the suggestion Cheong and Kristin. I will use this and let you know if this works for me.
    Friday, December 30, 2016 7:44 AM
  • Thanks for the suggestion Cheong and Kristin. I will use this and let you know if this works for me.

    You are welcome.

    Good luck!

    Have a nice day!

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 30, 2016 8:02 AM