编写XML输出 [英] Writing XML Output

查看:51
本文介绍了编写XML输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

我必须编写一个应用程序,该应用程序将读取文本文件,然后将该文件的内容输出为新的XML文件.

这是我必须使用的文本文件的一个片段.文本文件的结构将始终遵循相同的模式和结构;只有值会改变...

order.contact.email#bob@bob.com
order.contact.tel#0123 4567890
order.contact.fax#0123 4567891
order.enquiry#ABC1234
order.total-price#0.00
order.subtotal-price#0.00
order.total-tax#0.00
order.id#XYZ987


我已将其读入< list>使用StreamReader,它总共包含56行,我可以循环浏览并将相关的值分配给相关的XML节点.

如您所见,每个节点的值都在每行中的#之后.我已经使用VS2005的XSD工具为每个节点以及XSD架构(order.xsd)生成了一个空的XML框架(order.xml).

生成的XML文件中可能需要重复的区域,例如PARTS组中的一个或多个单个PART,其中PART具有各种属性,例如

Hello.

I''ve got to write an application that will read in a text file then output the contents of that file as a new XML file.

This is a snippet of the text file I have to work with. The structure of the text file will always follow the same pattern and structure; only the values will change...

order.contact.email#bob@bob.com
order.contact.tel#0123 4567890
order.contact.fax#0123 4567891
order.enquiry#ABC1234
order.total-price#0.00
order.subtotal-price#0.00
order.total-tax#0.00
order.id#XYZ987


I''ve read that into a <list> using StreamReader, it contains 56 lines in total thinking that I can loop through and assign the relevant value to the relevant XML node.

As you can see, the value for each node follows the # in each line. I''ve generated an empty XML skeleton (order.xml) for each node and also the XSD schema (order.xsd) using VS2005''s XSD tool.

There may be areas in the resulting XML file which need to repeated such as one or more single PART in a PARTS group where PART has various properties such as

...
   <PARTS>
     <PART>
       <MATERIAL>Material 1</MATERIAL>
       <NAME>Name 1</NAME>
     </PART>
     <PART>
       <MATERIAL>Material 2</MATERIAL>
       <NAME>Name 2</NAME>
     </PART>
    </PARTS>
...


我将每个节点都大写,因为我不能包括尖括号和歉意(如果您难以阅读的话).

我不知道的是如何将整个事情联系在一起.请有人能指出我的构建方向吗?

致以最诚挚的问候

Scott


I''ve capitalized each node as I can''t include the angled brackets, apologies if you find it hard to read.

What I can''t figure out is how to tie the whole thing together. Please can someone point me in the direction of how to build this?

With best regards

Scott

推荐答案

有很多方法可以将数据转换为XML,而Xml序列化可能是最简单的方法,假设从中读取数据时文本文件,您正在创建对象,而不仅仅是尝试将字符串值直接转换为XML.

当我必须做这样的事情时,我创建一个对象来保存数据,并提供一个XElement属性,该属性允许我使用XElement对象设置/获取对象数据.这使得它易于维护,并且可以使所有零碎的xml内容保持良好的本地化.他对我的方法秘诀是:

使用System.Xml.Linq;

XDocumentXElementXAttribute对象确实使XML的编写变得容易.我还写了一个技巧/窍门,描述了一些我想出的扩展方法,以使获取/设置值更加容易:

使用扩展方法来避免XML问题 [ ^ ]

编辑===============

没错,.Net 2.0没有Linq.以太坊中应该有很多XML帮助器类可用.您进行一些明智的谷歌搜索可能会大大有助于找到答案.很多时候,Google都会在CodeProject的此处公开适用的文章.
There are a number of ways to get the data into XML, and Xml serialization would probably be the easiest way, assuming that when you read your data in from the text file, you''re creating objects, and not just trying to go right from the string values to XML.

When I have to do something like this, I create an object to hold the data, and provide a XElement property that allows me to set/get the object data using a XElement object. This makes it easy to maintain, and keeps all the nigly xml stuff localized to a good degree. he secret to my method is this:

using System.Xml.Linq;

The XDocument, XElement, and XAttribute objects really make doing XML easy. I also wrote a tip/trick that describes some extension methods I came up with to make getting/setting values easier:

Using Extension Methods To Avoid XML Problems[^]

EDIT ===============

You''re right, .Net 2.0 doesn''t have Linq. There should be a lot of XML helper classes available out there in the ether. Some judicious googling on your part would probably go a long way toward finding an answer. many times, google will expose applicable articles right here on CodeProject.


嘿,
无需担心架构的另一种简便方法是遍历记录并将记录添加到数据表行中.然后只需调用
hey ,
another easy way in which you will not have to worry about the schema is that loop through the record and add the records to a datatable rows. and then simply call the
Datatable.WriteXML("C://test.xml"); 


方法.它将在指定位置写入XML文件.
祝你好运
Ahsan Sarfraz


method. it will write the XML file on the specified location.
Best of luck
Ahsan Sarfraz


这篇关于编写XML输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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