0

The articleabs webrequest method output’s id, when the POST request successful, which is forwarded createURL metod. However, I am getting the following error:

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

Code line:

var document = XDocument.Parse(articleABS());

I have tried adding in a null statement condition to the createURL method, if the id from the article method is empty then exit the method, but that has not worked either and I am still getting the error.

 public string createURL()
    {
        string uri = "";

        var document = XDocument.Parse(articleABS());
        var href = document.Descendants("link").Single().Attribute("title").Value;
        href = href.Replace("Article", "");
        string id = Regex.Match(href, @"\d+").Value;
        var result = (@"#####" + id + "/sections");
        return uri = result.ToString();
    }

    public string articleABS()

    {
        var result = (@"####" + mainID + "/articles");

        string URL = result;
        string method = "POST";
        string postData = articleXML.createArticleABS();

        try
        {

            if (hwrequest.Method == "POST")
            {
               ....
            }
            using (HttpWebResponse hwresponse = (HttpWebResponse)hwrequest.GetResponse())
            {
               ....
                }

                hwresponse.Close();

            }

        }
        catch (WebException e)
        {
            .....
            else
            {
                //
            }
        }

        return responseData;

    }

Example output of articleABS();

<?xml version="1.0" encoding="UTF-8"?> 
    <?xml-stylesheet href="https://api.newsweaver.com/nwapistatic/ntsa-api.xsl" type="text/xsl"?> 
    <?xml-stylesheet href="https://api.newsweaver.com/nwapistatic/ntsa-api.css" type="text/css"?> 
    <status>
        <code>409
        </code>
        <message>
            content.article.required.title null
        </message>
    </status>

Please advise. Thank you

MethodMan
  • 18,625
  • 6
  • 34
  • 52
user3070072
  • 610
  • 14
  • 37
  • What is the form of the return value of articleABS()? XDocument.Parse is going to expect well-formed XML; if articleABS() only returns an undecorated ID, that's exactly the sort of error you'll get. – Brian A. Henning Jun 20 '16 at 16:44
  • 1
    That exception indicates the xml you are trying to parse is malformed. You'll need to post the xml so we can see what is wrong with it. – Kevin Jun 20 '16 at 16:45
  • Thank you for comment. I have added my output response from the articleabs() method. I think the reason for the error is, the XML does allow articleXML() method to be null...? – user3070072 Jun 20 '16 at 16:51
  • 1
    Does this help: http://stackoverflow.com/questions/17947238/why-data-at-the-root-level-is-invalid-line-1-position-1-for-xml-document – kjhughes Jun 20 '16 at 16:52
  • 1
    Given your XML [is parsed with no errors](https://dotnetfiddle.net/RG3yxy), then I'd suggest your method doesn't return what you say it does. As ever, a [mcve] is required. – Charles Mager Jun 20 '16 at 18:10

0 Answers0