0

Trying to open this test file, which exists in a folder under my solution:

C:\dev\trunk\Development\WebSvc\WCFProj\Xml\po.xml

My c# method is :

 public XmlDocument generateXmlResponse()
        {
            string appDir = AppContext.BaseDirectory;
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(@"Xml\ResponseTempl.xml");

            return xml;
        }

and the exception message is :

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

Now when I step thru the code, I can use the relative path :

appDir + @"Xml\po.xml"

which correctly resolves to:

C:\dev\trunk\Development\WebSvc\WCFProj\Xml\po.xml

and I just took the sample PurchaseOrder.xml from the ms website https://msdn.microsoft.com/en-us/library/bb343181(v=vs.110).aspx :

<PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">
  <Address Type="Shipping">
    <Name>Ellen Adams</Name>
    <Street>123 Maple Street</Street>
    <City>Mill Valley</City>
    <State>CA</State>
    <Zip>10999</Zip>
    <Country>USA</Country>
  </Address>
  <Address Type="Billing">
    <Name>Tai Yee</Name>
    <Street>8 Oak Avenue</Street>
    <City>Old Town</City>
    <State>PA</State>
    <Zip>95819</Zip>
    <Country>USA</Country>
  </Address>
  <DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes>
  <Items>
    <Item PartNumber="872-AA">
      <ProductName>Lawnmower</ProductName>
      <Quantity>1</Quantity>
      <USPrice>148.95</USPrice>
      <Comment>Confirm this is electric</Comment>
    </Item>
    <Item PartNumber="926-AA">
      <ProductName>Baby Monitor</ProductName>
      <Quantity>2</Quantity>
      <USPrice>39.98</USPrice>
      <ShipDate>1999-05-21</ShipDate>
    </Item>
  </Items>
</PurchaseOrder>
bob.mazzo
  • 5,183
  • 23
  • 80
  • 149
  • 1
    `XmlDocument.LoadXml()` is used to load an Xml Snippet. Use [`XmlDocument.Load`](https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.load(v=vs.110).aspx) to load a file. – StuartLC Feb 06 '18 at 15:27
  • 1
    Yes indeed. thank you. You should have posted that answer, actually. – bob.mazzo Feb 06 '18 at 15:48
  • 1
    I'm sure there's a dupe out there somewhere - I used to mix these up all the time. Out of interest, you can consider [XDocument](https://stackoverflow.com/q/1542073/314291), i.e. the LINQ friendly successor to XmlDocument? – StuartLC Feb 06 '18 at 15:55
  • @StuartLC - yes that also works. I can load the xml file using `XDocument.Load(appDir + @"Xml\ResponseTempl.xml");` – bob.mazzo Feb 06 '18 at 16:23

1 Answers1

1

The issue here is that the following line:

xml.LoadXml(@"Xml\ResponseTempl.xml");

Is trying to load the string as XML and giving you the error as it is invalid XML. LoadXml should be used like so:

xml.LoadXml("<item><name>wrench</name></item>");

Since you are trying to read from a file you need to use: https://msdn.microsoft.com/en-us/library/875kz807(v=vs.110).aspx

This will look like the following:

xml.Load(@"Xml\ResponseTempl.xml");
Aaron
  • 1,361
  • 1
  • 13
  • 30