-2

The error i get is Daya at the root level is invalid. Line 1, position 1.

using System;
using System.Xml;
using System.IO;
namespace XmlReaderConsoleAPP
{
class Program
{
    static void Main()
    {
       
        XmlDocument xml = new XmlDocument();
        xml.LoadXml("c:\\SaintGobain_Pam_20210118.xml");
        string _byteOrderMarkUtf8 = 
        Enconding.UTF8.GetString(Encoding.UTF8.GetPreamble());
        if (xml.StartWith(_byteOrderMarkUtf8))
        {
            var lastIndexOfUtf8 = _byteOrderMarkUtf8.Length - 1;
            xml = xml.Remove(0, _byteOrderMarkUtf8.Length - 1);
        }

       
        XmlNodeList xnLista = xml.SelectNodes("/Batches/Batch/BatchFields/BatchField/Documents/Document/IndexFields/IndexField/Pages/Page");
        foreach(XmlNode xn in xnLista)
        {


            string formulario = xn["FormTypeName"].InnerText;
            string Value = xn["Value"].InnerText;
            string FileName = xn["ImportFileName"].InnerText;
            Console.WriteLine("Formulário: {0}", formulario, "Value: {1} ", Value, "FilePath: {2}", FileName);
        }
    }

}

}

And the XML contents are

<ImportSession>
<Batches>
<Batch Name="MSG_SaintGobainPam_20210118" Description="SaintGobain_20210118" 
   BatchClassName="SAINTGOBAIN - Faturas Notas" Processed="1">
  <BatchFields>
    <BatchField Name="CAPALOTE" Value="0" />
    <BatchField Name="NCONTENTOR" Value="0" />
  </BatchFields>
  <Documents>
    <Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
      <IndexFields>
        <IndexField Name="ETIQUETA" Value="ZE80047804" />
      </IndexFields>
      <Pages>
        <Page 
    ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047804.pdf" />
      </Pages>
    </Document>
    <Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
      <IndexFields>
        <IndexField Name="ETIQUETA" Value="ZE80047842" />
      </IndexFields>
      <Pages>
        <Page 
    ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047842.pdf" />
      </Pages>
    </Document>
    <Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
      <IndexFields>
        <IndexField Name="ETIQUETA" Value="ZE80047843" />
      </IndexFields>
      <Pages>
        <Page 
    ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047843.pdf" />
      </Pages>
    </Document>
    <Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
      <IndexFields>
        <IndexField Name="ETIQUETA" Value="ZE80047849" />
      </IndexFields>
      <Pages>
        <Page 
     ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047849.pdf"/>
       </Pages>
    </Document>
    <Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
      <IndexFields>
        <IndexField Name="ETIQUETA" Value="ZE80047853" />
      </IndexFields>
      <Pages>
        <Page 
    ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047853.pdf" />
      </Pages>
    </Document>
    <Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
      <IndexFields>
        <IndexField Name="ETIQUETA" Value="ZE80047854" />
      </IndexFields>
      <Pages>
        <Page 
    ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047854.pdf" />
      </Pages>
    </Document>
    <Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
      <IndexFields>
        <IndexField Name="ETIQUETA" Value="ZE80047855" />
      </IndexFields>
      <Pages>
        <Page 
    ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047855.pdf" />
      </Pages>
    </Document>
    <Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
      <IndexFields>
        <IndexField Name="ETIQUETA" Value="ZE80047860" />
      </IndexFields>
      <Pages>
        <Page 
     ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047860.pdf"/> 
     ErrorCode="20" ErrorMessage="Illegal file format 
     (\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047860.pdf)" />
      </Pages>
    </Document>
    <Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
      <IndexFields>
        <IndexField Name="ETIQUETA" Value="ZE80047861" />
      </IndexFields>
      <Pages>
        <Page 
       
      ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047861.pdf"/>
      </Pages>
    </Document>
  </Documents>
</Batch>

Im asking this because i couldnt find a answer to my question but if there is please link it in the answers. XML FILE is now added to the post as you can see. I really dont know why it keeps happening but ye hope you guys can help.

Tiago Silva
  • 254
  • 2
  • 13
  • 3
    The error message is complaining about your xml, but you did not post it. So it's hard to tell what may be wrong with it. – René Vogt Jan 19 '21 at 10:43
  • 1
    already answered here... https://stackoverflow.com/questions/17795167/xml-loaddata-data-at-the-root-level-is-invalid-line-1-position-1 – MD. RAKIB HASAN Jan 19 '21 at 10:45
  • Does this answer your question? [xml.LoadData - Data at the root level is invalid. Line 1, position 1](https://stackoverflow.com/questions/17795167/xml-loaddata-data-at-the-root-level-is-invalid-line-1-position-1) – Fildor Jan 19 '21 at 10:48
  • 1
    Please add the respective xml content. – Fildor Jan 19 '21 at 12:10
  • And it's missing a closing `` – Fildor Jan 19 '21 at 12:24
  • Ok, I got it to work with `XmlNodeList nodes = doc.SelectNodes(@"//Pages/Page");`: https://dotnetfiddle.net/DWDVY3 – Fildor Jan 19 '21 at 12:41
  • Note that there is what seems to be an error: ` ErrorCode='20' ErrorMessage='Illegal file format (\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047860.pdf)' /> ` That's not well-formed xml – Fildor Jan 19 '21 at 12:45
  • OOoooohhhh ... I am so blind ... and you are using [XmlDocument.LoadXml](https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.loadxml?view=net-5.0), which expects as parameter: _"String containing the XML document to load."_ - that means the _content_ not the filepath. You need to use [XmlDocument.Load](https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.load?view=net-5.0#System_Xml_XmlDocument_Load_System_String_), which expects: _"URL for the **file containing the XML document** to load. The URL can be either a local file or an HTTP URL (a Web address)."_ – Fildor Jan 19 '21 at 12:51
  • i guess its possible it as errors in the xml file but i didnt make it my coworker asked me to make this app on my very first actual day of being and intern so ye im kinda lost i never worked with xml files or anything the thing right now is that i still get the same error @Fildor – Tiago Silva Jan 19 '21 at 12:56
  • Did you replace `LoadXml` with `Load` method? – Fildor Jan 19 '21 at 13:01
  • It may throw an exception if the xml is invalid or any parsing errors occur. I'll suggest some code in an answer. – Fildor Jan 19 '21 at 13:03
  • 1
    @Fildor Thank you Fildor its now working i found the error i hope i can keep learning this stuff cause its quite fun – Tiago Silva Jan 19 '21 at 13:04
  • Yeah it's always fun'n'games until the input file comes that is totally messed up, but "cannot be fixed on client's side" ... :D – Fildor Jan 19 '21 at 13:16
  • haha maybe some day ill know what thats like – Tiago Silva Jan 19 '21 at 13:17
  • @Fildor i have tried to make that code work but my Enconding, StartWith and .Remove gives out a error and i cant take out that error cause thats gonna be used in the future for ticket making to be fixed in the other app – Tiago Silva Jan 19 '21 at 14:15

1 Answers1

1

Some suggestions to move forward:

static void Main()
{
   
    XmlDocument xml = new XmlDocument();
    // Catch Exception, print message and bail out.
    try{
        //      vv "LoadXml" replaced by "Load"
        xml.Load("c:\\SaintGobain_Pam_20210118.xml");
    }
    catch(Exception ex)
    {
        Console.WriteLine("OOps: {0}", ex.Message);
        return;
    }
    string _byteOrderMarkUtf8 = 
    Enconding.UTF8.GetString(Encoding.UTF8.GetPreamble());
    if (xml.StartWith(_byteOrderMarkUtf8))
    {
        var lastIndexOfUtf8 = _byteOrderMarkUtf8.Length - 1;
        xml = xml.Remove(0, _byteOrderMarkUtf8.Length - 1);
    }
                                            // vv replace XPath expression
    XmlNodeList xnLista = xml.SelectNodes(@"//Pages/Page");
    foreach(XmlNode xn in xnLista)
    {
        string formulario = xn["FormTypeName"].InnerText;
        string Value = xn["Value"].InnerText;
        string FileName = xn["ImportFileName"].InnerText;
        // Correct syntax:
        Console.WriteLine("Formulário: {0}, Value: {1} ,FilePath: {2}",formulario, Value, FileName);
    }
}

Then manually sanitize (for now) the source file:

Delete ErrorCode="20" ErrorMessage="Illegal file format (\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047860.pdf)" />

And give it another try.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • 1
    Sure will just gotta finish lunch break haha – Tiago Silva Jan 19 '21 at 13:18
  • i have tried to make that code work but my Enconding, StartWith and .Remove gives out a error and i cant take out that error cause thats gonna be used in the future for ticket making to be fixed in the other app – Tiago Silva Jan 19 '21 at 14:06