简体   繁体   English

创建一个动态xml文档

[英]Creating a dynamic xml document

Hi you is it possible to create a dynamica xml with xdocument I've been trying but it appears that it returns an exception about having a wrong structure 嗨,您可以使用我一直在尝试的xdocument创建一个dynamica xml,但它似乎返回有关结构错误的异常

My code is the following 我的代码如下

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();
        }

What i would like to acomplish by using this code is to get an xml like this 我想通过使用此代码完成的是获得这样的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>

For any answer or help thank you so much 对于任何答案或帮助,非常感谢

You are adding multiple roots to your document. 您正在向文档添加多个根。 You initially add one here: 您最初在此处添加一个:

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

And later add more root elements in a loop here: 然后在这里在循环中添加更多根元素:

                            responseXml.Add(dato);

However, each XML document must have exactly one single root element . 但是, 每个XML文档必须只有一个根元素 Thus you probably want to do: 因此,您可能想要执行以下操作:

                            responseXml.Root.Add(dato);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM