C ++ Windows窗体应用程序和XML [英] C++ Windows Forms Application and XML

查看:108
本文介绍了C ++ Windows窗体应用程序和XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我一直在努力解决这个问题,所以我请求一点帮助。

我正在开发一个应用程序,我差不多完成了。

我只需要将一些东西解析成XML文件并从中解析。

我尝试过使用tinyxml和tinyxml2,rapidxml等等,但是我没有在我的项目中使用它们。 />
我试图使用其他各种选项,但我也失败了。

我没有预编译的头文件,所以这不是问题,我也需要连接器链接。



我试图用button1_Click在XML文件中将一个简单的字符串从textBox1保存到第1行,如果存在则替换,

和还可以使用button2_Click从XML文件加载第1行字符串并在textBox1中显示它。



我不知道现在要去哪里...

我的脑袋已经爆炸了,似乎每个XML库和解决方案都与Windows Forms Applications无关,或者我正在做什么w rong with it。

Hello, I have been struggling with this for a while so I request a little help.
I am working on an application, with which I''m almost finished.
I just need to parse some things into XML file and from it.
I have tried using tinyxml and tinyxml2, rapidxml and so, but I have failed to use them inside my project.
I have tried to use various other options, but I also failed.
I don''t have precompiled headers so that can''t be problem, I also have required linkers linked.

I am trying to save a simple string from textBox1 to line 1 in XML file with button1_Click, replace if exists,
and also to load line 1 string from XML file with button2_Click and show it in textBox1.

I have no idea where to head now...
my head is already exploding, and it seems that every XML library and solution isn''t related to Windows Forms Applications, or I am doing something wrong with it.

推荐答案

不要使用这些第三方代码!一切都是在.NET FCL中完成的:



Don''t use those 3rd party codes! Everything is done for you in .NET FCL:



  1. 使用 System.Xml .XmlDocument 类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].





-SA


Sergey Alexandrovich Kryukov的解决方案可以很好地与Managed C ++建立链接。



我知道有3个似乎支持使用xsds(有人还认为CDATA部分是最好的解决方案吗?)。如果你不需要模式验证,那么你可以忽略下一步:



xerces

这是一个很大的,没有样本学习相当尴尬(谢天谢地,谷歌有很多),但可能是你可以获得的功能最丰富,最自由的解析器。



libxml2

如果你看一下相当骇人听闻的在线帮助和缺乏最基本的样本,这是一场噩梦。正如预期的那样小咆哮 - 它是一个Linux库。我没看过,但可能有一个端口。



msxml

这是一个更多在MFC应用程序中解析xml的常用方法。它是基于COM的,并且确实具有64位支持。我从来没有真正使用它,但作为一个成熟的微软图书馆,应该有相当多的在线帮助。



-



或者,在codeproject上有一些不那么臃肿的xml解析器:

CXMLFile - 一个简单的C ++ XML解析器 [ ^ ]

简单的C ++ XML解析器 [ ^ ]

如果搜索codeproject,还会有更多。



从您的描述中,听起来您正在尝试找到能够为您完成所有事情的一键式解决方案。也许我错了,但是对于xml解析器,你必须自己做一些编码。解析时,最简单的解析器通常至少需要以下内容:

a)将xml文件/字符串数据加载到解析器中

b)运行解析器

c)读取输出

一些解析器会为你构建一组很好的对象,你可以阅读,而其他的则更多参与,即你必须将解析后的数据移动到你写的对象中你自己。



我自己使用过tinyxml,发现它还可以 - 它工作正常。也许您可以解释为什么您使用的xml解析器实际上失败了?
The solution by Sergey Alexandrovich Kryukov should be fine for linking with Managed C++.

There are 3 that I know of which seem to support the use of xsds (does anyone still think CDATA sections are the best solution?). If you don''t need schema validation, then you can ignore the next bit:

xerces
This is big and fairly awkward to learn without samples (thankfully, there are many in googleland), but is probably the most feature-rich and free parser you can get.

libxml2
This is a nightmare if you look at the rather appalling on-line help and lack of samples beyond the most basic. As expected from this little rant - it is a Linux library. I haven''t looked, but there may be a port.

msxml
This was a more common way to parse xml in MFC applications. It was COM based and does have 64-bit support. I have never actually used it, but as a "mature" Microsoft library, there should be a fair amount of help on-line.

--

Alternatively, there are a few less "bloated" xml parsers right here on codeproject:
CXMLFile - A Simple C++ XML Parser[^]
Simple C++ XML Parser[^]
If you search codeproject, there will be plenty more.

From your description, it sounds like you are trying to find something that is a 1-click solution that does everything for you. Maybe I am wrong about this, but with xml parsers, you have to do some coding yourself. The most simple parsers usually demand the following at a minimum when parsing:
a) load an xml file/string data into the parser
b) run the parser
c) read the output
Some parsers will build a nice set of objects for you that you can read, while others are more involved, i.e. you have to move the parsed data into objects you have written yourself.

I have used tinyxml myself and found it to be ok - it worked fine. Perhaps you could explain why your used xml parsers actually failed?


这篇关于C ++ Windows窗体应用程序和XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆