如何在根级别修复数据无效

时间:2014-06-14 19:19:52

标签: c# .net xml

当我执行我的应用程序时,我收到一个错误:

我有以下xml doc:

<?xml version="1.0" encoding="utf-8"?>
<W-TIBCPTRs>
  <W-TIBCPTR>
    <TYPTRT>FDR2 R</TYPTRT>
    <CLAFCNO VALIDE="NON">5b1</CLAFCNO>
    <NUMCLI>0067781</NUMCLI>
    <TYPACT>D</TYPACT>
    <NATCTRT>STD</NATCTRT>
    <CLDEVCDE>EUR</CLDEVCDE>
    <AFCACH>511</AFCACH>
    <NUMACH>3700167</NUMACH>
    <DEVACH>EUR</DEVACH>
    <NUMDOC>20130051                           </NUMDOC>
    <LIBECR>D�l�gation signature(s)                                     </LIBECR>
    <GAPROFIL>JFL       </GAPROFIL>
    <GADATE>20130521</GADATE>
    <GAHEURE>113122</GAHEURE>
    <FILLER />
  </W-TIBCPTR>
  <W-TIBCPTR>
    <TYPTRT>FDR2 R</TYPTRT>
    <CLAFCNO>511</CLAFCNO>
    <NUMCLI>0068078</NUMCLI>
    <TYPACT>D</TYPACT>
    <NATCTRT>STD</NATCTRT>
    <CLDEVCDE>EUR</CLDEVCDE>
    <AFCACH>511</AFCACH>
    <NUMACH>2717150</NUMACH>
    <DEVACH>EUR</DEVACH>
    <NUMDOC />
    <LIBECR />
    <GAPROFIL />
    <GADATE />
    <GAHEURE />
    <FILLER />
  </W-TIBCPTR>
  <W-TIBCPTR>
    <TYPTRT>FDR2 V</TYPTRT>
    <CLAFCNO>511</CLAFCNO>
    <NUMCLI>0048606</NUMCLI>
    <TYPACT>D</TYPACT>
    <NATCTRT>STD</NATCTRT>
    <CLDEVCDE>EUR</CLDEVCDE>
    <AFCACH>511</AFCACH>
    <NUMACH>3129386</NUMACH>
    <DEVACH>EUR</DEVACH>
    <NUMDOC>fa2651                             </NUMDOC>
    <LIBECR>Non abouti/Facture � pr�-relancer                           </LIBECR>
    <GAPROFIL>JFL       </GAPROFIL>
    <GADATE>20130523</GADATE>
    <GAHEURE>142823</GAHEURE>
    <FILLER />
  </W-TIBCPTR>
  <W-TIBCPTR>
    <TYPTRT>FDR2 R</TYPTRT>
    <CLAFCNO>511</CLAFCNO>
    <NUMCLI>0069023</NUMCLI>
    <TYPACT>D</TYPACT>
    <NATCTRT>STD</NATCTRT>
    <CLDEVCDE>EUR</CLDEVCDE>
    <AFCACH>511</AFCACH>
    <NUMACH>3768193</NUMACH>
    <DEVACH>EUR</DEVACH>
    <NUMDOC>FC70                               </NUMDOC>
    <LIBECR>D�l�gation signature(s)                                     </LIBECR>
    <GAPROFIL>JFL       </GAPROFIL>
    <GADATE>20130524</GADATE>
    <GAHEURE>133716</GAHEURE>
    <FILLER />
  </W-TIBCPTR>
  <W-TIBCPTR>
    <TYPTRT>FDR2 R</TYPTRT>
    <CLAFCNO VALIDE="NON">5b8</CLAFCNO>
    <NUMCLI>0052830</NUMCLI>
    <TYPACT>E</TYPACT>
    <NATCTRT>GPE</NATCTRT>
    <CLDEVCDE>EUR</CLDEVCDE>
    <AFCACH>999</AFCACH>
    <NUMACH>0026627</NUMACH>
    <DEVACH>EUR</DEVACH>
    <NUMDOC>130512                             </NUMDOC>
    <LIBECR>D�l�gation signature(s)                                     </LIBECR>
    <GAPROFIL>JFL       </GAPROFIL>
    <GADATE>20130527</GADATE>
    <GAHEURE>095045</GAHEURE>
    <FILLER />
  </W-TIBCPTR>
</W-TIBCPTRs>

当我尝试通过c#访问它时:

string csvSeparator = ",";
            Func<string, string> escapeValue = val => val;

            string xml = textBox1.Text;
            XDocument doc = XDocument.Parse(xml);

            var headers = doc.Root
                            .Elements()
                            .First()
                            .Elements()
                            .Select(el => el.Name.LocalName);

            var headerRow = string.Join(csvSeparator, headers);

            var rows = from el in doc.Root.Elements()
                       let values = from prop in el.Elements()
                                    select escapeValue(prop.Value)
                       let row = string.Join(csvSeparator, values)
                       select row;


            IEnumerable<string> csvLines = new[] { headerRow }.Concat(rows);

我收到此错误:

Data at the root level is invalid. Line 1, position 1

这条线怎么了?

当我有像这行一样的属性字符串时,我还需要在excel中自动着色

<CLAFCNO VALIDE="NON">5b1</CLAFCNO>

1 个答案:

答案 0 :(得分:0)

我猜textBox1.Text包含xml文件的名称,而不是xml本身

所以,将XDocument.Parse(xml);更改为XDocument.Load(xml);