创建动态 xml 文档

Creating a dynamic xml document

您好,您是否可以使用 xdocument 创建一个 dynamica xml 我一直在尝试,但它似乎 returns 一个关于结构错误的异常

我的代码如下

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>

非常感谢任何答案或帮助

您正在向您的文档添加多个根目录。您最初在此处添加一个:

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

然后在此处循环添加更多根元素:

                            responseXml.Add(dato);

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

                            responseXml.Root.Add(dato);