The XML file generated using infopath form will append a specific piece of information at the beginning of the file to identify the version of the XML file generated by infopath form, the storage location of the corresponding xsn template, as shown in:
After loading the XML file using the xdocument. Load () method, how can we get the relevant node information in the file header? For example, MSO-infopathsolution.
In the xmlnodetype type, processinginstruction declares? Pi test?>" You can use this attribute to filter XML nodes. The following function is used to find the file header information in the infopath form XML file and modify the href attribute value in the MSO-infopathsolution node, change the location of the xsn template to the corresponding location in another server.
1 /// <Summary>
2 /// Replace the reference URL with the target server name for each infopath XML file.
3 /// </Summary>
4 /// <Param name = "path"> </param>
5 /// <Param name = "targetserver"> </param>
6 Public Static Void Replacereferenceurl ( String Path, String Targetserver)
7 {
8 Const String Pattern = " Href = \ "http ://.*? / " ; // Regular Expression used for replacing the Reference URL in the XML files.
9 Directoryinfo info = New Directoryinfo (PATH );
10 Fileinfo [] files = info. getfiles ( " *. Xml " );
11
12 Foreach (Fileinfo File In Files)
13 {
14 Xdocument document = xdocument. Load (file. fullname );
15 List <xnode> List = Document. nodes (). Where (t => T. nodetype = system. xml. xmlnodetype. processinginstruction). tolist <xnode> ();
16 If (List! = Null )
17 {
18 Foreach (Xnode item In List)
19 {
20 Xprocessinginstruction TMP = item As Xprocessinginstruction;
21 If (TMP. Target. Equals ( " MSO-infopathsolution " ))
22 {
23 RegEx Reg = New RegEx (pattern );
24 TMP. Data = reg. Replace (TMP. Data, " Href = \ "http :// " + Targetserver + " / " );
25 Document. Save (file. fullname );
26 Break ;
27 }
28 }
29 }
30 }
31
32 Directoryinfo [] direcotries = info. getdirectories ();
33 Foreach (Directoryinfo directory In Direcotries)
34 {
35 Replacereferenceurl (directory. fullname, targetserver );
36 }
First, use the lambda expression to find all nodes of the processinginstruction type, and then use a regular expression to replace the value of the href attribute (note that only the part of servername is replaced ). The following recursive call indicates that this method allows you to modify all XML files in the specified directory.
In Sharepoint, infopath form is used to collect XML data files in many places. When you need to upload infopath XML files in batches, it is necessary to modify the file header information. Through the above function, we can easily achieve this! Record it for future reference.