在转换为CSV

时间:2015-06-10 15:35:57

标签: c# xml wpf parsing csv

因此,我目前在C#WPF应用程序中使用以下代码段将某些XML数据转换为CSV。

string text = File.ReadAllText(file);
text = "<Root>" + text + "</Root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(text);
StreamWriter write = new StreamWriter(FILENAME1);
XmlNodeList rows = doc.GetElementsByTagName("XML");

foreach (XmlNode row in rows)
{
    List<string> children = new List<string>();

    foreach (XmlNode child in row.ChildNodes)
    {
        children.Add(child.InnerText.Trim());
    }

    write.WriteLine(string.Join(",", children.ToArray()));
}

然而,我遇到了一个情况。我的输入XML数据类似于以下内容(抱歉,您必须水平滚动以查看数据在原始格式中的实际外观):

<XML><HEADER>1.0,770162,20121009133435,3,</HEADER>20121009133435,721,5,1,0,0,0,00:00,00:00,<EVENT>00032134826064957,4627,</EVENT><DRUG>1,1872161156,7,0,10000</DRUG><DOSE>1,0,5000000,0,10000000,0</DOSE><CAREAREA>1 </CAREAREA><ENCOUNTER></ENCOUNTER><ADVISORY>Keep it simple or spell
    tham ALL out. For some reason 
    that is not the case
    please press the on button 
    when trying to activate
    device codes also available on
list</ADVISORY><CAREGIVER></CAREGIVER><PATIENT></PATIENT><LOCATION>20121009133435,00-1d-71-0a-71-80,-66</LOCATION><ROUTE></ROUTE><SITE></SITE><POWER>0,50</POWER></XML> 

现在,我遇到的问题是......我的输出看起来像这样(如下所示);因为,它是一个CSV文件,我希望输出在一行中,那么我如何从原始数据中删除换行符,以便输出在一个水平线上?我迷失了如何处理这种情况。 Replace(System.Environment.NewLine, "")会有效吗?任何帮助将不胜感激!

1.0,770162,20121009133435,3,,20121009133435,721,5,1,0,0,0,00:00,00:00,,00032134826064957,4627,1,,1872161156,7,0,10000,1,0,5000000,0,10000000,0,1 ,,Keep it simple or spell
    tham ALL out. For some reason 
    that is not the case
    please press the on button 
    when trying to activate
    device codes also available on
list,,,20121009133435,00-1d-71-0a-71-80,-66,,,0,50 

编辑:

另请注意,我的输入文件有几千行,如下所示:

<XML><HEADER>1.0,770162,20121009133435,3,</HEADER>20121009133435,721,5,1,0,0,0,00:00,00:00,<EVENT>00032134826064957,4627,</EVENT><DRUG>1,1872161156,7,0,10000</DRUG><DOSE>1,0,5000000,0,10000000,0</DOSE><CAREAREA>1 </CAREAREA><ENCOUNTER></ENCOUNTER><ADVISORY>Keep it simple or spell
        tham ALL out. For some reason 
        that is not the case
        please press the on button 
        when trying to activate
        device codes also available on
    list</ADVISORY><CAREGIVER></CAREGIVER><PATIENT></PATIENT><LOCATION>20121009133435,00-1d-71-0a-71-80,-66</LOCATION><ROUTE></ROUTE><SITE></SITE><POWER>0,50</POWER></XML> 
<XML><HEADER>2.0,773162,20121009133435,3,</HEADER>20121004133435,761,5,1,0,0,0,00:00,00:00,<EVENT>00032134826064957,4627,</EVENT><DRUG>1,18735166156,7,0,10000</DRUG><DOSE>1,0,5000000,0,10000000,0</DOSE><CAREAREA>1 </CAREAREA><ENCOUNTER></ENCOUNTER><ADVISORY>Keep it simple or spell
        tham ALL out. For some reason 
        that is not the case
        please press the on button 
        when trying to activate
        device codes also available on
    list</ADVISORY><CAREGIVER></CAREGIVER><PATIENT></PATIENT><LOCATION>20121009133435,00-1d-71-0a-71-80,-66</LOCATION><ROUTE></ROUTE><SITE></SITE><POWER>0,50</POWER></XML> 

.. goes on 

1 个答案:

答案 0 :(得分:1)

尝试

children.Add(Regex.Replace(child.InnerText, "\\s+", " "));

这不应该依赖于任何特定的换行符,也会消除每行之间的四个空格。 \s是任何空格的正则表达式,+表示一次或多次出现。