将.xml文件转换为.cs文件 [英] converting .xml file to .cs file

查看:159
本文介绍了将.xml文件转换为.cs文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想知道有什么方法可以将XML文件转换为等效的类文件.我已经使用了VS2008中提供的XSD.EXE工具,但它不能满足我的要求.如果我使用XSD.EXE,则需要对自动生成的类文件进行手动更改,因为XML文件中的每个元素都具有在自动生成的类中创建的等效集合.这将不灵活.任何人都可以帮助我.如果没有其他选择,那么我是否必须编写此转换过程的代码?

任何信息将不胜感激.预先感谢.

Hi,
I wanted to know is there any way by which i can convert an XML file into an equivalent class file. I have used XSD.EXE tool provided in VS2008, but it is not meeting my requirements. If i use XSD.EXE, I need to do manual changes to that auto generated class file, since every element in XML file has equivalent collection, created in the auto generated class. This will not be flexible. So can anyone help me out. If there is no other go then do i have to code this conversion process?.

Any information will be appreciated. Thanks in advance.

推荐答案

这不是转换".您需要反序列化XML数据.

您是否已经为该文件定义了XML模式?你能改变吗?我会说,如果可以,请使用Data Contract( http://msdn.microsoft.com/en -us/library/ms733127.aspx [ ^ ]).它会以标准方式自动序列化/反序列化由任何数据类型(不一定是树)组成的任何对象图.文件格式"将即时创建.

如果已经提供了XML数据,则需要或多或少手动编码.有几种不同的方法:

This is not "convert". You need to deserialize XML data.

Do you have XML schema for the file already defined? Can you change it? If you could, I would say, use Data Contract (http://msdn.microsoft.com/en-us/library/ms733127.aspx[^]). It would automatically serialize/deserialize any object graphs consisting of any data types, not necessarily tree, in a standard manner. The file "format" would be created on the fly.

If the XML data is already given, you will need to code it more or less manually. There are several different ways:


  1. 使用System.Xml.XmlDocument类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
    请参见
  2. 使用类System.Xml.XmlTextReader; library/system.xml.xmldocument.aspx"target =" _ blank"title =" New Window> ^ ].
  3. 使用类System.Xml.XmlTextReader;这是最快的读取方法,尤其是您需要跳过一些数据.
    请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ 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



—SA


首先,我认为您应该有一个应用所有XML文件的通用模式文件.这样,您可能会从XSD工具获得更好的结果.但是,您不应高估它的功能,因为它会完全忽略内容内部文本属性值等,并以标记/属性名称为目标来生成相应的代码.
如果您正在寻找更多的定制方法,请寻找文本生成方法. Ruby erb是我所知道的功能强大的工具之一.例如
Generator.rb
To begin with, I think you should have a common schema file that applies all of the XML files. Then you may get better results from the XSD tool. But you should not overestimate its capabilities as it completely ignores the content-inner text-attribute values etc and targets tag/attribute names to generate the corresponding code.
If you are looking for more customised approach look for text generation methods. Ruby erb is one of the powerful ones that I know. For instance
Generator.rb
require 'erb'
require 'rexml/document'
include REXML

def subClsHdr (superName,clsNode)
  fh = File.open("header.erb", "r" )
  eruby_script_header = fh.read
  fc = File.open("source.erb", "r" )
  eruby_script_cpp = fc.read
  name = clsNode.attributes['Name']
  fhw = File.open("#{name}.h","w")
  fcw = File.open("#{name}.cpp","w")
  fields = []
  clsNode.elements.each('Field'){|f|
    fields.push( [f.attributes['Type'], f.attributes['Name']]) }
  clsNode.elements.each('Class'){|f| subClsHdr(name,f)}
  erb = ERB.new(eruby_script_header)
  code = erb.result( binding )
  fhw.print code
  fhw.close
  erb = ERB.new(eruby_script_cpp)
  code = erb.result( binding )
  fcw.print code
  fcw.close
end

file = File.new("test.xml")
doc = Document.new(file)
doc.elements.each('Class'){|t| subClsHdr("",t)}


header.erb


header.erb

/** Author: yesotaso
  * File: "<%= name.capitalize %>.h"
  * Creation Date: <%= Time.now.to_s %>
  * This file is created by automated code generation script
  * via reading xml input. Any changes made will disappear
  * upon next code generation.
  */
#ifndef <%= name.upcase %>_H_
#define <%= name.upcase %>_H_<%if superName.length > 0 %>
#include "<%= superName %>.h"<%end%>
#ifndef COMMONS_H_
    #include "Commons.h"
#endif
class <%= name %><%if superName.length > 0 %> : public <%= superName %><% end %>
{
public:
    <%= name %>();
    virtual ~<%= name %>();
<%fields.each{|l|%> const <%=l[0]%> get<%=l[1].capitalize%>();
    void set<%=l[1].capitalize%>(const <%=l[0]%>& param);<%}%>
private:
<%fields.each{|l|%> <%=l[0]%> <%=l[1]%>;<%}%>};
#endif //<%= name.upcase %>_H_


然后输入


And input

<Class Name="Person">
    <Field Name="Name" Type="std::string"></Field>
    <Field Name="Status" Type="std::string"></Field>
    <Field Name="Age" Type="int"></Field>
    <Class Name="Worker">
        <Field Name="ssid" Type="int"></Field>
        <Class Name="Banker">
            <Field Name="Position" Type="std::string"></Field>
        </Class>
    </Class>
</Class>


输出"Person.h":


The output "Person.h":

/** Author: yesotaso
  * File: "Person.h"
  * Creation Date: xxxxxxxxxxxxxxx
  * This file is created by automated code generation script
  * via reading xml input. Any changes made will disappear
  * upon next code generation.
  */
#ifndef PERSON_H_
#define PERSON_H_
#ifndef COMMONS_H_
    #include "Commons.h"
#endif
class Person
{
public:
    Person();
    virtual ~Person();
    const std::string getName();
    void setName(const std::string& param);
    const std::string getStatus();
    void setStatus(const std::string& param);
    const int getAge();
    void setAge(const int& param);
private:
    std::string Name;
    std::string Status;
    int Age;
};
#endif //PERSON_H_


输出"Worker.h":


Output "Worker.h":

/** Author: yesotaso
  * File: "Worker.h"
  * Creation Date: xxxxxxxxxxxxx
  * This file is created by automated code generation script
  * via reading xml input. Any changes made will disappear
  * upon next code generation.
  */
#ifndef WORKER_H_
#define WORKER_H_
#include "Person.h"
#ifndef COMMONS_H_
	#include "Commons.h"
#endif
class Worker : public Person
{
public:
	Worker();
	virtual ~Worker();
	const int getSsid();
	void setSsid(const int& param);
private:
	int ssid;
};
#endif //WORKER_H_


依此类推,并带有相应的"cpp"文件等.您当然可以选择Linq to Xml或其他适合您口味的方法.
希望对您有所帮助.


And so on with corresponding "cpp" files etc. You can of course go for Linq to Xml or some other way whichever fits your taste.
Hope it helps.


这篇关于将.xml文件转换为.cs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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