locked
Reading and Parsing an InfoPath XML document on the SharePoint 2007 RRS feed

  • Question

  • User304012707 posted

    I need to open and read an xml document from a web page which happens to be an InfoPath form. The form is on my PC. What is giving me fits being able to parse the nodes to get values. I'm not sure how to refer to the nodes to get the values. There is a lot of documentation on connecting to SharePoint to do this, which is not an option. I've posted a sample of the XML from the InfoPath form to be able to read it and get the values into variables. At this point, I'm just trying to read the nodes ( need the elements ) in the document.

    Here is my code. There is an error "There are multiple root elements." at the position "doc.Load(url);".  Please make any suggestions.

    string url="http://test/test.xml";
    XmlDocument doc = new XmlDocument();
    XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = CredentialCache.DefaultCredentials;
    doc.XmlResolver = resolver;
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
    nsmgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-07-03T15:53:42");
    nsmgr.AddNamespace("xd", "http://schemas.microsoft.com/office/infopath/2003");
    nsmgr.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");           
    doc.Load(url);
    XPathNavigator xfield = null;
    XPathNavigator myRoot = doc.CreateNavigator();
    xfield = myRoot.SelectSingleNode("/my:myFields/my:Subject", nsmgr);

    test.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Documentation:-myXSD-2008-07-03T15-53-42" solutionVersion="1.0.0.171" productVersion="12.0.0.0" PIVersion="1.0.0.0" href="http://test/Forms/template.xsn"?>
    <?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
    <my:myFields
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-07-03T15:53:42"
    xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="zh-EN">
      <my:Subject>
    subject name
      </my:Subject>
      <my:group2>
        <my:group3>
          <my:field2></my:field2>
        </my:group3>
      </my:group2>
      <my:group5>
        <my:group6>
          <my:field15></my:field15>
        </my:group6>
      </my:group5>
      <my:group7>
        <my:group8>
          <my:field16></my:field16>
          <my:field17></my:field17>
          <my:field19></my:field19>
          <my:field20></my:field20>
        </my:group8>
      </my:group7>
      <my:group9>
        <my:group10>
          <my:AttachFile xsi:nil="true"></my:AttachFile>
        </my:group10>
      </my:group9>
    </my:myFields>
    Tuesday, March 17, 2015 11:22 AM

Answers

  • User1508394307 posted

    Again, I tested you code against your xml - it returns "subject name", which is correct.

    So, if you get an error on doc.Load() it either xml is different (not the one as per your example) or it cannot load http://test/test.xml, redirected to some other page and cannot open it.

    You could do a quick test and read xml (and check if it is correct) and then load it to XmlDocument

    String strResult;
    WebResponse objResponse;
    WebRequest objRequest = HttpWebRequest.Create(strURL);
    objResponse = objRequest.GetResponse();
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
    {
       strResult = sr.ReadToEnd();
       sr.Close();
    }
    
    ... check here if strResult is correct xml
    //doc.Load(url); doc.LoadXml(strResult);

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 18, 2015 8:00 AM

All replies

  • User1508394307 posted

    geminiiv

    read an xml document from a web page which happens to be an InfoPath form. The form is on my PC.

    I'm confused with what does "my PC" here means

    If you want to access xml which is on client then that's not possible. 

    Or you mean you want to access remote xml from http://test/test.xml?

    P.S. The code seems to be correct: https://dotnetfiddle.net/m3sytM

    Output 

    subject name

    Tuesday, March 17, 2015 11:39 AM
  • User304012707 posted

    Yes. I want to access remote xml from http://test/test.xml

    But I don't open your url:  https://dotnetfiddle.net/m3sytM 

    Could you please insert the code to here? Thank you!

    Tuesday, March 17, 2015 2:04 PM
  • User1508394307 posted

    The code is exactly the same and it works. 

    It seems that your code cannot open http://test/test.xml and that's the reason you got an error.  

    Tuesday, March 17, 2015 2:36 PM
  • User304012707 posted

    If the code cannot open http://test/test.xml, I would get a error:" The remote server returned an error: (401) Unauthorized".

     

    But there is an error "There are multiple root elements." at the position "doc.Load(url);". The error means the code should be open http://test/test.xml?

    Tuesday, March 17, 2015 11:30 PM
  • User1508394307 posted

    Again, I tested you code against your xml - it returns "subject name", which is correct.

    So, if you get an error on doc.Load() it either xml is different (not the one as per your example) or it cannot load http://test/test.xml, redirected to some other page and cannot open it.

    You could do a quick test and read xml (and check if it is correct) and then load it to XmlDocument

    String strResult;
    WebResponse objResponse;
    WebRequest objRequest = HttpWebRequest.Create(strURL);
    objResponse = objRequest.GetResponse();
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
    {
       strResult = sr.ReadToEnd();
       sr.Close();
    }
    
    ... check here if strResult is correct xml
    //doc.Load(url); doc.LoadXml(strResult);

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 18, 2015 8:00 AM
  • User304012707 posted

    Thank you very much.

    The reason is SharePoint authority.

     
    Thursday, March 19, 2015 8:24 AM