字符串数据到xml的转换 [英] string data to xml convertion

查看:82
本文介绍了字符串数据到xml的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
    我有一个字符串格式的数据,我想将其另存为xml文件,例如
字符串数据:
keyborard; 175.56; 2008; HP;   ] productName,price,modelyear,companyName
Monitor; 20.85; 2009; Dell

现在我想转换为xml

< product> KeyBorad< product \ >
< price> 175.56<价格>
< model> Dell< model>
< company> HP< company>


我该怎么做


是我

hello
      i have a data in an string formate , i want to save it as xml file

for example
string data:
keyborard;175.56;2008;HP;                        productName,price,modelyear,companyName
Monitor;20.85;2009;Dell

Now i want to convert to xml

<product>KeyBorad <product\>
<price> 175.56 <price>
<model>Dell<model>
<company> HP <company>


how can i do this


It's Me

推荐答案

        string ToXMLFormat(string fileName)
        {
            string ret = "";
            using (StreamReader sr = File.OpenText(fileName))
            {
                string header = sr.ReadLine();//reading the header
                string[] headers = header.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string record = sr.ReadLine();
                while (null != record)
                {
                    string[] values = record.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int hi = 0; hi < headers.Length; hi++)
                    {
                        ret += string.Format("<{0}>", headers[hi]);
                        ret += values[hi];
                        ret += string.Format("</{0}>", headers[hi]);
                        ret += "\r\n";
                    }
                    record = sr.ReadLine();
                }
            }
            return ret;
        }


这篇关于字符串数据到xml的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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