locked
System.Xml.XmlException: Root element is missing. RRS feed

  • Question

  • User1491413197 posted

    Hi there,

    Am trying to create a conctact form where in the webservice i have a method call insert.

    But when i invoke the method i get an error:

    System.Xml.XmlException: Root element is missing.
       at System.Xml.XmlTextReaderImpl.Throw(Exception e)
       at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res)
       at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
       at System.Xml.XmlTextReaderImpl.Read()
       at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
       at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
       at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
       at System.Xml.XmlDocument.Load(XmlReader reader)
       at System.Xml.XmlDocument.Load(String filename)
       at Service.insert(String firstname, String lastname, String age, String StreetAddress, String City, String State, String Country, String PostalCode, String phonenumber, String mobilenumber, String officenumber, String emailaddress) in c:\Users\Jay\Documents\Visual Studio 2013\WebSites\WebSite3\App_Code\Service.cs:line 31

    Here is my code >

    [WebMethod]
    public string insert(string firstname, string lastname, string age, string StreetAddress, string City, string State, string Country, string PostalCode, string phonenumber, string mobilenumber, string officenumber, string emailaddress)
    {
    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load("C:/Users/Jay/Documents/Visual Studio 2013/WebSites/WebSite3/App_Data/contact.xml");
    XmlNode rootNode = xmlDoc.DocumentElement;
    String save = "";
    try
    {
    XmlNode userNode = xmlDoc.CreateElement("ContactXML");
    rootNode.AppendChild(userNode);

    XmlNode FirstName = xmlDoc.CreateElement("FirstName");
    FirstName.InnerText = firstname;
    userNode.AppendChild(FirstName);

    XmlNode LastName = xmlDoc.CreateElement("LastName");
    LastName.InnerText = lastname;
    userNode.AppendChild(LastName);

    XmlNode Age = xmlDoc.CreateElement("Age");
    Age.InnerText = age;
    userNode.AppendChild(Age);

    XmlNode Address = xmlDoc.CreateElement("Address");
    XmlNode streetAddress = xmlDoc.CreateElement("streetAddress");
    streetAddress.InnerText = StreetAddress;
    XmlNode city = xmlDoc.CreateElement("city");
    city.InnerText = City;
    XmlNode state = xmlDoc.CreateElement("State");
    state.InnerText = State;
    XmlNode country = xmlDoc.CreateElement("Country");
    country.InnerText = Country;
    XmlNode postalCode = xmlDoc.CreateElement("postalCode");
    postalCode.InnerText = PostalCode;
    Address.AppendChild(streetAddress);
    Address.AppendChild(city);
    Address.AppendChild(state);
    Address.AppendChild(country);
    Address.AppendChild(postalCode);
    userNode.AppendChild(Address);

    XmlNode ContactNumber = xmlDoc.CreateElement("ContactNumber");
    XmlNode PhoneNumber = xmlDoc.CreateElement("PhoneNumber");
    PhoneNumber.InnerText = phonenumber;
    XmlNode MobileNumber = xmlDoc.CreateElement("MobileNumber");
    MobileNumber.InnerText = mobilenumber;
    XmlNode OfficeNumber = xmlDoc.CreateElement("OfficeNumber");
    OfficeNumber.InnerText = officenumber;
    XmlNode EmailAddress = xmlDoc.CreateElement("EmailAddress");
    EmailAddress.InnerText = emailaddress;
    ContactNumber.AppendChild(PhoneNumber);
    ContactNumber.AppendChild(MobileNumber);
    ContactNumber.AppendChild(OfficeNumber);
    ContactNumber.AppendChild(EmailAddress);
    userNode.AppendChild(ContactNumber);

    save = "Contact succesfully added";
    xmlDoc.Save("C:/Users/Jay/Documents/Visual Studio 2013/WebSites/WebSite3/App_Data/contact_xml.xml");

    }
    catch (Exception ex)
    {
    save = "Access Denied!";

    }

    return save;

    }

    Monday, April 13, 2015 12:17 PM

Answers

  • User1711366110 posted


    when i invoke the method i get an error:
    System.Xml.XmlException: Root element is missing.

      As per this case, the issue might be that XML file is not properly formatted and that it was missing a root element.
    For instance, your XML looking may like this:

    <?xml version='1.0' encoding='UTF-8'?>
    <element>A</element>
    <element>B</element>
    <element>C</element>

    your xml will be like this :

    <?xml version='1.0' encoding='UTF-8'?>
    <elements>
      <element>A</element>
      <element>B</element>
      <element>C</element>
    </elements>

    So check the sequence of rootNode.AppendChild(..) & userNode.AppendChild(..)

    --
    with regards,
    Edwin

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2015 5:29 AM

All replies

  • User-484054684 posted

    Check the xml document's inner XML in the debugger on this line:

    xmlDoc.Save("C:/Users/Jay/Documents/Visual Studio 2013/WebSites/WebSite3/App_Data/contact_xml.xml");

    That should give the clue.

    Probably the namespace is missing or XML tags are missing, etc. So, inspecting the xml that is saved in this object "xmlDoc" would give the clue of what is going wrong.

    Monday, April 13, 2015 1:17 PM
  • User1711366110 posted


    when i invoke the method i get an error:
    System.Xml.XmlException: Root element is missing.

      As per this case, the issue might be that XML file is not properly formatted and that it was missing a root element.
    For instance, your XML looking may like this:

    <?xml version='1.0' encoding='UTF-8'?>
    <element>A</element>
    <element>B</element>
    <element>C</element>

    your xml will be like this :

    <?xml version='1.0' encoding='UTF-8'?>
    <elements>
      <element>A</element>
      <element>B</element>
      <element>C</element>
    </elements>

    So check the sequence of rootNode.AppendChild(..) & userNode.AppendChild(..)

    --
    with regards,
    Edwin

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2015 5:29 AM