Append Node to XML - InfoPath Dev
in

InfoPath Dev

Append Node to XML

Last post 02-12-2007 03:22 PM by Patrick Halstead. 15 replies.
Page 1 of 2 (16 items) 1 2 Next >
Sort Posts: Previous Next
  • 01-30-2007 10:08 AM

    Append Node to XML

    Hello Everyone,
    I have created a form in infopath that i am hosting in a visual studio
    2005 project. The XML that is generated from the form gets saved into
    a database .. this all works fine. Recently I've added some
    functionality onto my project in Visual Studio 2005 c# code and i want
    to append an XML node that i create to the XML that is generated from
    my form when a certain button clicks. When i try and do this i get an
    error ..see below for the code and the error message. I have no idea
    what this means or how to fix it. I tried modifying the schema but
    that didn't work, also there could be any number of nodes to append.
    An example of the nodes to append are:

    <Images> -- the parent node, this will always be appended onto the xml
    from the form
    <Ink>VALUE HERE</Ink>
    <Photo>VALUE HERE</Photo> -- there could be zero to many
    different ink and photo nodes applied on the fly
    </Images>

    Error Message:
    Schema validation found non-data type errors

    Here is the code i use to append the new xmlnode to the xml the form
    generates.

    XPathNavigator xn =
    formControl1.XmlForm.MainDataSource.CreateNavigator();
    XPathNavigator navigator =
    xn.SelectSingleNode(Constants.MYFIELDS_FIELD, xmlnsm);

    XmlElement elementToAppend = prepareXmlNodeForAppend("<Images/>");

    navigator.AppendChild(elementToAppend.OuterXml);

    ------------------------------------

    public XmlElement prepareXmlNodeForAppend(string xmlData)
    {
    XmlDocument xDoc = new XmlDocument();

    byte[] bytes = Encoding.UTF8.GetBytes(xmlData);

    using (MemoryStream memStream = new MemoryStream())
    {
    // Load the byte array into the memory stream
    memStream.Write(bytes, 0, bytes.Length);

    // Load the memory stream into the FormControl
    memStream.Seek(0, SeekOrigin.Begin);
    xDoc.Load(memStream);

    // Close the memory stream
    memStream.Close();
    }
    return xDoc.DocumentElement;
    }
  • 01-30-2007 10:46 AM In reply to

    Hi ErinSelena and welcome to our forum,
    Images and ink must be base64 encoded. You should search for "InfoPath images base64 BASE64" on Google. There are a couple good blog posts on the Microsoft site about them and we have numerous forum posts on them as well. Good luck!
    Patrick Halstead
    Project Manager at Qdabra
  • 01-30-2007 11:26 AM In reply to

    Hi Patrick,
    Thanks so much for replying. I don't think I was very clear, the actual image and ink data will be stored in my database, the value inside my XML will be the ID of the row the image or ink is stored in. At this point all I've tried to do is append an empty node onto my form XML and that's when this error occurs... I am at a loss as to what to do.. can this be done???
  • 01-30-2007 11:28 AM In reply to

    Probably you need to select the namespace. Please search on that.
    Patrick Halstead
    Project Manager at Qdabra
  • 01-30-2007 11:30 AM In reply to

    Hi Patrick,
    What exactly do you mean by Select the namespace... sorry i'm new to this type of thing.
  • 01-30-2007 01:32 PM In reply to

    This is driving me nuts, there has to be a way to do this ... can anybody help?
  • 01-30-2007 01:46 PM In reply to

    Hi Erin, sorry I don't have more time. Search this forum (upper right) for "select namespace" and see if some of the posts make sense. If not, I'll ask Greg to chime in.
    Patrick Halstead
    Project Manager at Qdabra
  • 01-30-2007 01:55 PM In reply to

    If you've verified that your function (prepareXmlNodeForAppend) is actually returning a valid XML node, then the problem is likely that you are not allowed, because of your schema, to add the node.

    Was your data source created inside InfoPath? If so, then just add the nodes you want to your data source so that they are valid when you try to append them.

    If the data source is secondary and you created it, then make the appropriate updates. If you did not create it, then whatever source it came from needs to make the updates.
  • 01-31-2007 05:46 AM In reply to

    Hi Greg,
    Thanks SO MUCH for replying. I will verify everything that you have said in your post and get back to you today whether or not this has worked. The problem is that i have no idea how many ink or photo nodes there are going to be, there could be any number of them so i can't really add them to the datasource.
    I will be in touch soon.
    Erin
  • 01-31-2007 09:18 AM In reply to

    You can make them repeating in your data structure. Then it doesn't matter how many you need to add.
  • 01-31-2007 10:25 AM In reply to

    Sorry if this is a silly question but how do I add a repeating field to my datasource without having an actual control for it to databind to? And then how would i actually add a new row in my hosted app without action from my form?
  • 02-06-2007 04:29 PM In reply to

    Keep in mind that the data source is ENTIRELY independant from any view and control. If for example, you created your main data source using the InfoPath designer, then you would open the data source task pane, right click on an existing group or field and choose properties. From there you can specify that it is repeating. If you want to add a new one, right-click on the parent node, and choose Add. Then specify a repeating field or group.

    Adding new will not affect your form views at all. Changing a field or group to repeating might have an affect on your views if you already have controls bound to the changed node.
  • 02-08-2007 06:05 AM In reply to

    Hi Greg,
    OK i think i get it now, i have just added a new group to my datasource and 2 fields inside that group, the fields are repeating but the group is not. So now inside my hosting application (in visual studio 2005) how do i actually append a new node inside that repeating group.... I don't know why this is so complicated for me but i can't quite get my head around it.
    Thanks,
    Erin
  • 02-09-2007 12:17 PM In reply to

    I think I answered this question (vaguely enough) in another of your posts wherein I mentioned that you will need to create the nodes in code, and then insert of append them to the proper location.

    There should be plenty of tutorials online on how to do create an XmlNode and insert it into an existing DOM. Visual Studio help will probably also be of help here.
  • 02-12-2007 01:41 PM In reply to

    Hi Everyone,
    I ended up having to call Microsoft on this one. The repeating table I had was inside an optional section on my form. It turns out you cannot add nodes programmatically to optional sections, i turned my option section into a regular section and all works as it should.
    Erin
Page 1 of 2 (16 items) 1 2 Next >
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.