MFC C++ and parse XML file, best way?

Noah Aas 685 Reputation points
2025-02-28T15:47:21.33+00:00

Hello! What is the best way to read this in MFC C++?

What is the best way to write this in a XML file?

Like XDocument in C#

<ROOT>
	<SYSTEM lanes="2" isDispense="0" isPack="0"/>
	<MACHINE address="127.0.0.1" port="6000" identifier="33"/>
	<CLIENT port="7000" id="34"/>
	<CUSTOM  active="1" />
</ROOT>
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,875 questions
{count} votes

Accepted answer
  1. Darran Rowe 1,511 Reputation points
    2025-02-28T18:16:33.8133333+00:00

    With a little bit of fiddling around, Windows.Data.Xml is available. IIRC, this wraps MSXML, so that is always another option.

    One important point to note, even though this is a WinRT component, this is usable in desktop applications without any major issue. To make it really easy, C++/WinRT can be used. There is one very important point to be made though, this can have some unintended consequences with MFC applications, and it often better to place this functionality in a separate DLL. First, C++/WinRT changes some settings to make MIDL capable of compiling WinRT IDL files, if your application is creating COM components then this will have an effect. Secondly, if anything is directly or indirectly including Windows.Globalization.h then this will cause compiler errors when included alongside the MFC headers. There are ways around it, but it is better to just separate the functionality.


2 additional answers

Sort by: Most helpful
  1. Viorel 120.4K Reputation points
    2025-02-28T16:14:17.33+00:00

    I think that a good way is: to set the “Common Language Runtime Support” option to “.NET Framework Runtime Support (/clr)” in Project Properties (if possible), to add references to System.Xml and System.Xml.Linq assemblies, and to use the XmlDocument or XDocument classes like in C#.

    Or to make parts of the code in the form of C# libraries.


  2. Minxin Yu 13,001 Reputation points Microsoft External Staff
    2025-03-04T09:28:07.8033333+00:00

    Hi,

    In addition to MSXML,

    you can find popular XML libraries on GitHub.
    Such as TinyXML, PugiXML
    https://stackoverflow.com/a/62604389

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.