Access XML data of infopath

Source: Internet
Author: User
Tags xml xpath

Infopath
It is a data input tool based on XML technology. It consists of three parts: schema, XSLT, and XML)
. Its data is stored in XML format. However, the development interface provided by it encapsulates XML processing, instead of using xmldocument, it provides a set
Xdocument object.

In actual application scenarios, we generally use infopath as the client to input data and interact with the server.
Infopath must pass data to the server after receiving user input
Infopath must pass the data to the server after receiving the information entered by the user. Infopath can communicate with servers through multiple channels.
Servie.

How to select a data format is a problem. Because infopath's own data are in XML format, it is natural that the data will be sorted by XML format
After the column is made, it is passed between infopath and the server. If the data is simple, you can directly access the XML file on the server. However, infopath data is generally complex,
Contains many information fields. If you use the XML file access method, it is troublesome. In this case, we will consider converting the data into a data entity object, which makes access very convenient. Then
Infopath data access can be classified into two types:

First, direct access to XML data.

1. Use infopath's xdocument interface to access data

Here, we directly access the DOM attribute of the thisxdocument object, and all data is saved in this attribute. We can locate the data according to the XML XPath syntax, but we need to set the necessary attributes selectionlanguage and selectionnamespaces before accessing.

Read the value of a node
Public String data_getsinglenodevalue (string XPath)
{
(Ixmldomdocument2) thisxdocument. DOM). setproperty ("selectionlanguage", "XPath ");
(Ixmldomdocument2) thisxdocument. DOM). setproperty ("selectionnamespaces ",
"Xmlns: DFS = \" + "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" +
"\" Xmlns: My = \ "http://schemas.microsoft.com/office/infopath/2003/myxsd/2007-01-11t07-19-53\" +
"Xmlns: TNS = \" http://tempuri.org /\"");
Ixmldomnode node = (ixmldomdocument2%thisxdocument.dom%.doc umentelement. selectsinglenode (XPath );
If (null = node)
{
Throw new exception (string. Format ("can't locate the node by XPath {0}", XPath ));
}
Return node. text;
}

Set the value of a node

Public void data_setnodevalue (string XPath, string value)
{
(Ixmldomdocument2) thisxdocument. DOM). setproperty ("selectionlanguage", "XPath ");
(Ixmldomdocument2) thisxdocument. DOM). setproperty ("selectionnamespaces ",
"Xmlns: DFS = \" + "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" +
"\" Xmlns: My = \ "http://schemas.microsoft.com/office/infopath/2003/myxsd/2007-01-11t07-19-53\" +
"Xmlns: TNS = \" http://tempuri.org /\"");
Ixmldomnode node = (ixmldomdocument2%thisxdocument.dom%.doc umentelement. selectsinglenode (XPath );
If (null = node)
{
Throw new exception (string. Format ("can't locate the node by XPath {0}", XPath ));
}
If (node. Attributes. getnameditem ("xsi: Nil ")! = NULL)
{
Node. Attributes. removenameditem ("xsi: Nil ");
}
Node. Text = value;
}

2. Access data using the standard method of system. xml

On the server side, it is very easy to access the XML data passed by infopath. Load the XML passed by infopath into a standard xmldocument and access it, however, you must set the namespace first.

Public xmlnode parsedata (string input, string XPath)
{
/// Create a xmldocument to save the input
Xmldocument Doc = new xmldocument ();
Doc. loadxml (input );
/// Create an xmlnamespacemanager for resolving namespaces.
Xmlnamespacemanager nsmgr = new xmlnamespacemanager (Doc. nametable );
Nsmgr. addnamespace ("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-04-11T09:28:40 ");

Xmlelement root = Doc. documentelement;

/// Get basic info
Xmlnode node = root. selectsinglenode (XPath, nsmgr );
Return node;
}
 

Type 2: Access XML data through data objects.
This method is more suitable for data transmission between the client and the server or multiple servers. InProgramData is accessed by object, while data is transmitted by XML. What needs to be solved is the conversion between XML and data objects, that is, serialization and deserialization.
If the XML schema has been defined, Microsoft provides the XSD tool to automatically generate a data Class Based on the schema, greatly saving the work of developers. Xsd.exe can be found in Visual Studio Tools.

1. Convert schema to data class
Execute xsd.exe myschema. XSD/C in the command line
Its
Myschema. XSD is a data definition file generated by infopath. If you directly use infopath to design a form, you cannot find this file.
An xsn file is displayed. This xsn is actually the package installation file of a group of files. After you change the xsn suffix to cab, you can use WinZip to open it. Then you will see
Myschema. XSD.

2. Access data using data objects on the server

Convert XML to a data object. Doc is the xmldocuemnt passed by infopath. mytype is the class name generated by XSD,
Protected object deserializexmlfile (xmldocument doc, type mytype)
{
System. Text. utf8encoding uf8 = new system. Text. utf8encoding ();
Byte [] buffer = uf8.getbytes (Doc. innerxml );
Memorystream filestreamreader = new memorystream (buffer );
Xmlreader reader = xmlreader. Create (filestreamreader );
// Serial the XML to a object
Xmlserializer serializer = new xmlserializer (mytype );
Return serializer. deserialize (Reader );
}
Converts a data object to XML. myobject is the data object to be serialized, and mytype is the data class.
Protected string serializeobject (Object myobject, type mytype)
{
Memorystream filestream = new memorystream ();
Xmlwriter writer = xmlwriter. Create (filestream );

// Writer. writeprocessinginstruction (
Foreach (processinginstructioninfo info in processinginstructioninfolist)
{
Writer. writeprocessinginstruction (info. szprocessinginstructionname, info. szprocessinginstructiontext );
}
Xmlserializer serializer = new xmlserializer (mytype );
Serializer. serialize (writer, myobject );
System. Text. utf8encoding uf8 = new system. Text. utf8encoding ();
String xml = uf8.getstring (filestream. toarray ());
// Shit, remove? In line1 posiiton 1, I don't know where it come from?
Return XML. substring (1 );
}

3. Use Data Objects on infopath to access data
On the infopath end, you can also use data objects to access data. In this way, it is much easier to use data objects if you are responsible for data verification. However, in infopath, if you want to write the modified data object Value back to XML, some conversion work is required.
# Region "data convert between thisxdocument and dataobject ebceventdata"

Void testdataobj ()
{
Ebceventdata infopath = getdataobj ();
Infopath. myfields. projectid = "123 ";
Replacedocumentwithdataobj (infopath );
}

It is easy to convert thisxdocument data to a data object. Create an xmldocument, load thisxdocument. Dom. XML data, and deserialize it.
/// <Summary>
/// Get a dataobj from thisxdocument
/// </Summary>
/// <Returns> </returns>
Ebceventdata getdataobj ()
{
Xmldocument Doc = new xmldocument ();
Doc. loadxml (thisxdocument. Dom. XML );
Ebceventdata infopath = new ebceventdata (DOC );
Return infopath;
}
After modifying the Data Object content, you need to write the modification to thisxdocument. Because xdocument does not support direct modification to XML, it can only take a roundabout approach to retrieve the corresponding node from XML and replace the content of the corresponding node of xdocument.
/// <Summary>
/// Write the dataobj's data into thisxdocument's XML
/// </Summary>
/// <Param name = "dataobj"> </param>
Void replacedocumentwithdataobj (ebceventdata dataobj)
{
Xmldocument Doc = new xmldocument ();
Doc = dataobj. savetoxml ();
Ixmldomnode newnode = clonexmltoixmldoc (Doc. documentelement, thisxdocument. DOM );
Thisxdocument. Dom. replaceChild (newnode, thisxdocument.dom.doc umentelement );
}

Set xmldocument
Ixmldomnode of infopath. The two types of nodes are processed differently. All leaf nodes (xmlnodetype. Text =
Childnode. nodetype) directly copies the value of the node, instead of the non-leaf node (xmlnodetype. Element =
Childnode. nodetype) uses recursive replication of the node structure, that is, copying all the child nodes, and then copying the node value.
/// <Summary>
/// Get xmlnode of element and convert it into infopath's ixmldomnode
/// </Summary>
/// <Param name = "systemxmlelement"> </param>
/// <Param name = "msxmldocument"> </param>
/// <Returns> </returns>
Ixmldomnode clonexmltoixmldoc (xmlelement systemxmlelement, ixmldomdocument msxmldocument)
{
Ixmldomnode msxmlresultnode;

// Create a new element from the MSXML Dom using the same
// Namespace as the xmlelement.
Msxmlresultnode = msxmldocument. createnode (
Domnodetype. node_element,
Systemxmlelement. Name,
Systemxmlelement. namespaceuri );

If (systemxmlelement. haschildnodes)
{
Foreach (xmlnode childnode in systemxmlelement. childnodes)
{
Ixmldomnode child;
If (xmlnodetype. Element = childnode. nodetype)
{
Child = clonexmltoixmldoc (xmlelement) childnode, msxmldocument );
}
Else
{
If (xmlnodetype. Text = childnode. nodetype)
{
Child = getvalue (childnode, msxmldocument );
}
Else
{

Throw new exception (string. Format ("the node type: {0} can't be
Processed ", childnode. nodetype. tostring ()));
}
}
Msxmlresultnode. appendchild (child );
}
}
// Set the element's value.
If (null! = Systemxmlelement. value)
{
Msxmlresultnode. Text = systemxmlelement. value. tostring ();
}
Return msxmlresultnode;
}

/// <Summary>
/// Get xmlnode of textnode and convert it to infopath's ixmldomnode
/// </Summary>
/// <Param name = "Node"> </param>
/// <Param name = "msxmldocument"> </param>
/// <Returns> </returns>
Ixmldomnode getvalue (xmlnode node, ixmldomdocument msxmldocument)
{
Ixmldomnode msxmlresultnode;

Msxmlresultnode = msxmldocument. createnode (
Domnodetype. node_text,
Node. Name,
Node. namespaceuri );
// Set the element's value.
If (null! = Node. value)
{
Msxmlresultnode. Text = node. value. tostring ();
}
Return msxmlresultnode;
}
# Endregion

In net
2. 0. optional fields can be defined in XML. However, if the data type of the optional field is bool, it can be correctly converted when converting from XML to a data object, but the data object can be reserialized.
When it is XML, optional fields are ignored. Even if there is a value in it, the fields are also discarded in the re-generated XML. In this way, lossless data conversion is impossible. No good solutions have been found, so we have
Bool fields are defined as non-empty fields.

Taking my program as an example, two bool values are defined in my infopath data source. Isndafield is required, and isallowpicturefield is optional that can be empty.

The infopath schema is defined as follows:

<XSD: element name = "isnda" type = "XSD: Boolean"/>
<XSD: element name = "isallowpicture" nillable = "true" type = "XSD: Boolean"/>

In the data class automatically created by xsd.exe, the following results are generated:

Private bool isndafield; private bool isndafieldspecified; private system. nullable <bool> isallowpicturefield; private bool isallowpicturefieldspecified;

See xmlserializer, xsd.exe, nullable
And you. (http://jasonkemp.ca/archive/2005/10/31/2743.aspx)

 

From: http://blog.csdn.net/yanwei100/archive/2007/01/31/1498926.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.