以表格格式将xml文件转换为平面文件

时间:2017-02-07 06:32:13

标签: c# xml

我想要使用C#将许多XML展平并保存为文件(如下所示)。我尝试过的一个选项是使用excel导入文件,然后从c#调用vba脚本。有没有选择在C#中执行此操作。

示例输入xml:

<request>
    <log-date>11/28/2016 04:48:40</log-date>
    <service-name>getPdf</service-name>
    <request-id>1234</request-id>
    <request-xml>
    <MyRequest xmlns="http://abcd.com">
        <GroupID>123</GroupID>
        <ClientName>ACBD</ClientName>
        <BrokerInfo>
            <BrokerLoginName>9876</BrokerLoginName>
            <FullName>John</FullName>
        </BrokerInfo>
        <BrokerInfo>
            <BrokerLoginName>0987</BrokerLoginName>
            <FullName>Mike</FullName>
        </BrokerInfo>   
    </MyRequestRequest>
    </request-xml>
</request>

预期的输出文件:

log-date|service-name|request-id|groupID|ClientName|BrokerLoginName|FullName
11/28/2016 04:48:40|getPdf|1234|123|ACBD|9876|John
11/28/2016 04:48:40|getPdf|1234|123|ACBD|0987|Mike

1 个答案:

答案 0 :(得分:0)

以下是您的解决方案:

using System.IO;
using System.Xml;

        public static void ReadInnerText()
    {
        StreamWriter file = new StreamWriter("myTextFile.txt");
        file.WriteLine("log-date|service-name|request-id|groupID|ClientName|BrokerLoginName|FullName");
        string Line = string.Empty;
        string BrokerLoginName = string.Empty;
        XmlDocument doc = new XmlDocument();
        XmlNodeList SecondTag;
        XmlNodeList ThirdTag;
        XmlNodeList FourthTag;
        XmlNodeList FifthTag;
        doc.Load("inputFile.xml");
        XmlNodeList elemList = doc.GetElementsByTagName("request");
        foreach (XmlNode firstNode in elemList)
        {

            SecondTag = firstNode.ChildNodes;
            foreach (XmlNode SecondNode in SecondTag)
            {

                if (SecondNode.Name.Equals("log-date"))
                {
                    Line = SecondNode.InnerText + "|";
                }
                if (SecondNode.Name.Equals("service-name"))
                {
                    Line = Line + SecondNode.InnerText + "|";
                }
                if (SecondNode.Name.Equals("request-id"))
                {
                    Line = Line + SecondNode.InnerText + "|";
                }
                ThirdTag = SecondNode.ChildNodes;
                foreach (XmlNode ThirdNode in ThirdTag)
                {
                    FourthTag = ThirdNode.ChildNodes;
                    foreach (XmlNode FourthNode in FourthTag)
                    {
                        if (FourthNode.Name.Equals("GroupID"))
                        {
                            Line = Line + FourthNode.InnerText + "|";
                        }
                        if (FourthNode.Name.Equals("GroupName"))
                        {
                            Line = Line + FourthNode.InnerText + "|";
                        }
                        if (FourthNode.Name.Equals("ClientName"))
                        {
                            Line = Line + FourthNode.InnerText + "|";
                        }
                        FifthTag = FourthNode.ChildNodes;
                        foreach (XmlNode FifthNode in FifthTag)
                        {
                            if (FifthNode.Name.Equals("BrokerLoginName"))
                            {
                                BrokerLoginName = FifthNode.InnerText + "|";
                            }
                            if (FifthNode.Name.Equals("FullName"))
                            {    
                                file.WriteLine(Line+BrokerLoginName+FifthNode.InnerText);
                            }
                        }
                    }
                }
            }
        }
        file.Close();
    }