创建动态xml文档

时间:2015-03-24 01:36:26

标签: c# xml linq dynamic

您好我可以使用xdocument创建一个动态xml我一直在尝试,但它似乎返回了一个关于错误结构的例外

我的代码如下

public string ReadTest(Stream csvFile)
        {
            XDocument responseXml = new XDocument( new XDeclaration("1.0", "utf-8", "yes"));   

            try
            {
                if ( csvFile != null || csvFile.Length!=0)
                {
                    responseXml.Add(new XElement("root"));

                    //using(CsvFileReader reader=new CsvFileReader(File.OpenRead(@"C:\Users\toshibapc\Documents\Visual Studio 2013\Projects\WCFLecturaCSV\WCFLecturaCSV\App_Data\archivo.csv"))){
                    using (CsvFileReader reader = new CsvFileReader(csvFile))
                    {

                        CsvRow row = new CsvRow();
                        List<String> headers = new List<string>();


                        while (reader.ReadRow(row))
                        {
                            int cont = 0;

                            XElement dato = new XElement("AccountInfos", new XElement("Info"));
                            XElement datos=null;
                            foreach (String s in row)
                            {
                                if(s.Equals("AccountIDToMove", StringComparison.OrdinalIgnoreCase)|| s.Contains("AccountNameToMove") || s.Contains("NewParentAccountID") || s.Contains("NewParentAccountName")){
                                    headers.Add(s);                                    
                                }
                                else{
                                    if (s != String.Empty)
                                    {
                                        datos = new XElement(headers[cont], s); //.Append("<" + headers[cont] + ">" + s + "<" + headers[cont] + "/>");
                                        dato.Add(datos);
                                    }                                    
                                   }
                                cont++;                                
                            }
                            if (headers.Count == 4 && datos != null)
                                responseXml.Add(dato);

                        } // fin de while 
                    }

                } // Check if no file i sent or not info on file

            }
            catch (Exception ex) {
                //oError = ex.Message;
            }
            return responseXml.ToString();
        }

使用此代码我想要的是获得像这样的xml

<xml version="1.0">
<root>
<AccountInfos>
<Info>
<AccountIDToMove>312456</AccountIDToMove>
<AccountNameToMove>Burger Count</AccountNameToMove>
<NewParentAccountID>453124</NewParentAccountID>
<NewParentAccountName> Testcom sales 1</NewParentAccountName>
</Info>
<Info>
<AccountIDToMove>874145</AccountIDToMove>
<AccountNameToMove>Mac Count</AccountNameToMove>
<NewParentAccountID>984145</NewParentAccountID>
<NewParentAccountName> Testcom sales 1</NewParentAccountName>
</Info>
</AccountInfos>
</root>

任何答案或帮助都非常感谢

1 个答案:

答案 0 :(得分:0)

您正在为文档添加多个根。您最初在此处添加一个:

                responseXml.Add(new XElement("root"));

稍后在循环中添加更多根元素:

                            responseXml.Add(dato);

然而,each XML document must have exactly one single root element。因此,您可能想要这样做:

                            responseXml.Root.Add(dato);