InfoPath是微软提供的一个深好用的XML编辑工具,可以为此InfoPath编辑好各种各样表单的模板与数输入界面,然后其他人可以运用这个表单模板输入表单数据,提交至比如说ERP系统中去—因为其保存的数码是XML格式的,而且若可在InfoPath里面安装XML验证表单的方法,我会以另一样篇文章里介绍如何利用InfoPath创建一个表单模板并且使是模板。

 

例如下面是一个InfoPath生成的XML文件:

<?xml version="1.0" encoding="UTF-8"?>

<? color: blue; font-family: ‘Courier New’;"> solutionVersion="1.0.0.15" productVersion="12.0.0" PIVersion="1.0.0.0"

                       href="file:///C:"Documents%20and%20Settings"xyz"My%20Documents"Paper.xsn"

                       name="urn:schemas-microsoft-com:office:infopath:Paper:-myXSD-2005-10-21T21-12-27" ?>

<? color: blue; font-family: ‘Courier New’;"> progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>

<my:Paper 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/2005-10-21T21:12:27"

          xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="zh-cn">

 <my:Candidate>张三</my:Candidate>

 <my:PositionApplies>STE</my:PositionApplies>

 <my:ApplicationDate xsi:nil="true"></my:ApplicationDate>

 <my:FillInOffice>false</my:FillInOffice>

 <my:Questions>

    <my:Question>

      <my:QuestionText>asdfasdf</my:QuestionText>

      <my:Answers my:SelectedAnswer="false">

        <my:AnswerDescription>asfasfdsa</my:AnswerDescription>

      </my:Answers>

      <my:Answers my:SelectedAnswer="true">

        <my:AnswerDescription>afsafd</my:AnswerDescription>

      </my:Answers>

      <my:Answers my:SelectedAnswer="false">

        <my:AnswerDescription>afasfdf</my:AnswerDescription>

      </my:Answers>

      <my:Answers my:SelectedAnswer="false">

        <my:AnswerDescription>asfadsfsafd</my:AnswerDescription>

      </my:Answers>

      <my:Id>1</my:Id>

    </my:Question>

 </my:Questions>

 <my:Skill>C#</my:Skill>

</my:Paper>

 

假使拿地方的XML文件包的话,最直观的包方法就是是这样的了:

    public class Answers : List<Answer>

    {

    }

 

    public class Questions : List<Question>

    {

    }

 

    public class Answer

    {

        public bool SelectedAnswer { get; set; }

 

        public string AnswerDescription { get; set; }

    }

 

    public class Question

    {

        public Question()

        {

            Answers = new Answers();

        }

 

        public string QuestionText { get; set; }

 

        public Answers Answers { get; set; }

 

        public int Id { get; set; }

    }

 

    public class Paper

    {

        public Paper()

        {

            Questions = new Questions();

        }

 

        public string Candidate { get; set; }

 

        public string PositionApplies { get; set; }

 

        public DateTime ApplicationDate { get; set; }

 

        public bool FillInOffice { get; set; }

 

        public Questions Questions { get; set; }

 

        public string Skill { get; set; }

    }

 

至今为止,看起要一如既往切开完美,类型和性能都是直接照射到XML对应的节点和总体性上了,接着我们怀念用底的代码来尝试用好别的自定义的靶子序列化成一个XML文件,并且相比两个文件之内存是否形似:

// 省去了实例化自定义对象的一些代码

 

using (XmlWriter writer = XmlWriter.Create(@"c:"test.xml") )

{

      XmlSerializer xs = new XmlSerializer(typeof(Paper));

 xs.Serialize(writer, paper);

}

 

可是最终的序列化结果也无克于InfoPath打开,对比我们序列化生产的文件与InfoPath生成的文书,可以窥见少独文本重大的差别在对节点的处理,有些节点的内容可以吧空,而我辈于C#代码里面却定义成值类型—值类型不可知啊空值,所有的节点和性质都是以一个命名空间下面的(而我们通过序列化生成的文书是于默认的命名空间底下的)。在下表中,我将序列化生成的文件的差异用浅灰色标注出,而InfoPath文件之异样用黄色标注出:

序列化生成的文件

InfoPath原始文件

<?xml version="1.0" encoding="utf-8"?>

<Paper xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <Candidate>张三</Candidate>

 <PositionApplies>STE</PositionApplies>

 <ApplicationDate>2009-03-10T00:59:05.9912457+08:00</ApplicationDate>

 <FillInOffice>false</FillInOffice>

 <Questions>

    <Question>

      <QuestionText>This is a test question</QuestionText>

      <Answers>

        <Answer>

          <SelectedAnswer>false</SelectedAnswer>

          <AnswerDescription>Answer 1</AnswerDescription>

        </Answer>

        <Answer>

          <SelectedAnswer>true</SelectedAnswer>

          <AnswerDescription>Answer 2</AnswerDescription>

        </Answer>

        <Answer>

          <SelectedAnswer>false</SelectedAnswer>

          <AnswerDescription>Answer 3</AnswerDescription>

        </Answer>

        <Answer>

          <SelectedAnswer>false</SelectedAnswer>

          <AnswerDescription>Answer 4</AnswerDescription>

        </Answer>

      </Answers>

      <Id>0</Id>

    </Question>

 </Questions>

 <Skill>C#</Skill>

</Paper>

<?xml version="1.0" encoding="UTF-8"?>

<? background: yellow; color: blue; font-family: ‘Courier New’;"> solutionVersion="1.0.0.15" productVersion="12.0.0" PIVersion="1.0.0.0"

                       href="file:///C:"Documents%20and%20Settings"xyz"My%20Documents"Paper.xsn"                        name="urn:schemas-microsoft-com:office:infopath:Paper:-myXSD-2005-10-21T21-12-27" ?>

<? background: yellow; color: blue; font-family: ‘Courier New’;"> progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>

<my:Paper 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/2005-10-21T21:12:27"

             xmlns:xd=http://schemas.microsoft.com/office/infopath/2003

xml:lang="zh-cn">

 <my:Candidate>张三</my:Candidate>

 <my:PositionApplies>STE</my:PositionApplies>

 <my:ApplicationDate xsi:nil="true"></my:ApplicationDate>

 <my:FillInOffice>false</my:FillInOffice>

 <my:Questions>

    <my:Question>

      <my:QuestionText>asdfasdf</my:QuestionText>

      <my:Answers my:SelectedAnswer="false">

        <my:AnswerDescription>asfasfdsa</my:AnswerDescription>

      </my:Answers>

      <my:Answers my:SelectedAnswer="true">

        <my:AnswerDescription>afsafd</my:AnswerDescription>

      </my:Answers>

      <my:Answers my:SelectedAnswer="false">

        <my:AnswerDescription>afasfdf</my:AnswerDescription>

      </my:Answers>

      <my:Answers my:SelectedAnswer="false">

        <my:AnswerDescription>asfadsfsafd</my:AnswerDescription>

      </my:Answers>

      <my:Id>1</my:Id>

    </my:Question>

 </my:Questions>

 <my:Skill>C#</my:Skill>

</my:Paper>

 

内,我们好小心到双方的反差有下列几项,为了便利说明,下面的字以InfoPath文件简称I文件,而序列化文件简称S文件:

1.         I文件中所有的节点都当别名为my的命名空间里面.

2.         S文件没有<?mso* ?>的程序处理指令节点(ProcessInstruction
Node),这吗尽管是干吗咱们用序列化生成的XML文件在资源管理器里无可知经过双击用InfoPath打开文件.

3.         I文件中来有值类型的节点的值也空,而于C#中值类型是无克为空的.

4.         S文件里用Paper.Questions.Answers数组的Answer都作一个个单独的节点序列化了,而I文件当中,每一个C#的Answer对象还是因此Answers节点表示.

5.         I文件里,SelectedAnswer是一个Xml属性,而S文件当中,它可受序列化成一个Xml节点了。

 

则来诸如此类多的距离,但是幸运的是,我们的.NET Framework的XML序列化功能足够强大,强大到得为咱经过编制好少的代码用方4个出入去掉.

1.        
XmlSerializer提供了一个构造函数,允许我们以尽序列化的下,显示地加上要的命名空间支撑.然后您可以当路的各国一个域(Field)上面长对应之性表明你要以该域(Field)序列化到啊一个命名空间内去.例如下面代码

public class Question

{

    public Question()

    {

        Answers = new Answers();

    }

    [XmlElement(

"http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27")]

    public string QuestionText { get; set; }

    public Answers Answers { get; set; }

    public int Id { get; set; }

}

 

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

ns.Add("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27");

 

XmlSerializer xs = new XmlSerializer(typeof(Paper));

xs.Serialize(reader, paper, ns);

 

2.         这个差别.NET序列化没有供于序列化的时段写副程序处理指令信息的成效,因为序列化应该只是做序列化操作,其他的操作应该由另外的函数或者功能模块完成,为什么?想不通的口舌请去读一下资深的《Code
Complete》。但是咱可以通过以序列化的时,给XmlSeriailzer提供一个XmlWriter实例,序列化操作实践前,我们先行亮地写入程序处理指令信息,然后重新于XmlSerializer接着在XmlWriter当前底指针位置上马我们未竟的事业。

3.         这个可以经C# 2.0内部纵使提供的可空值类型(Nullable)来处理,如果您没动或者听说过只是空值类型(Nullable)的话,还是快去翻翻书吧,毕竟现在C#
4.0还将发布了……

4.        
XmlSerializer所在的命名空间供了无数性(Attribute)来控制序列化过程被节点写副的方法,XmlElement属性(Attribute)可以当序列化数组的上,让您有机遇指定数组中各个一个因素序列化到Xml文件中对应之节点名称。

5.         而XmlAttribute属性(Attribute)将XmlSerializer默认的把类型域序列化成一个Xml节点的操作修改成序列化成一个Xml属性,在将品种域序列化Xml属性的时,默认的行事是未会见助长也Xml属性加上命名空间的,因此我们用指定XmlAttribute的Form域设置成XmlSchemaForm.Qualified,这样以序列化的时段,XmlSerializer就见面自动吗Xml属性加上命名空间了。

 

脚是完好的序列化和倒序列化的源代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Xml.Serialization;

using System.Xml;

using System.Xml.Schema;

 

namespace TestSerialization

{

    public class Answers : List<Answer>

    {

    }

 

    public class Questions : List<Question>

    {

    }

 

    public class Answer

    {

        [XmlAttribute(Form = XmlSchemaForm.Qualified)]

        public bool SelectedAnswer { get; set; }

 

        public string AnswerDescription { get; set; }

    }

 

    public class Question

    {

        public Question()

        {

            Answers = new Answers();

        }

 

        public string QuestionText { get; set; }

 

        [XmlElement]

        public Answers Answers { get; set; }

 

        public int Id { get; set; }

    }

 

    public class Paper

    {

        public Paper()

        {

            Questions = new Questions();

        }

 

        public string Candidate { get; set; }

 

        public string PositionApplies { get; set; }

 

        public DateTime? ApplicationDate { get; set; }

 

        public bool FillInOffice { get; set; }

 

        [XmlArray]

        public Questions Questions { get; set; }

 

        public string Skill { get; set; }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            Paper paper = new Paper

            {

                ApplicationDate = DateTime.Now,

                Candidate = "Shi Yimin",

                PositionApplies = "STE I"

            };

 

            Question question = new Question();

            question.QuestionText = "This is a test question";

            question.Answers.Add(new Answer { AnswerDescription = "Answer 1", SelectedAnswer = false });

            question.Answers.Add(new Answer { AnswerDescription = "Answer 2", SelectedAnswer = true });

            question.Answers.Add(new Answer { AnswerDescription = "Answer 3", SelectedAnswer = false });

            question.Answers.Add(new Answer { AnswerDescription = "Answer 4", SelectedAnswer = false });

 

            paper.Questions.Add(question);

            paper.Skill = "C#";

 

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27");

            ns.Add("xd", "http://schemas.microsoft.com/office/infopath/2003");

           

            using (XmlWriter writer = XmlWriter.Create(@"c:"test.xml") )

            {

                writer.WriteProcessingInstruction("">                    "solutionVersion=""1.0.0.15"" productVersion=""12.0.0"" " +

                    "PIVersion=""1.0.0.0"" href=""file:///C:""Documents%20and%20Settings""v-yishi""My%20Documents""Paper.xsn"" " +

                    "name=""urn:schemas-microsoft-com:office:infopath:Paper:-myXSD-2005-10-21T21-12-27""");

                writer.WriteProcessingInstruction("">                    "progid=""InfoPath.Document"" versionProgid=""InfoPath.Document.2""");

                XmlSerializer xs = new XmlSerializer(typeof(Paper), "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27");

                xs.Serialize(writer, paper, ns);

            }

 

            using ( FileStream fs = new FileStream(@"E:"家庭作业"InterviewForm.xml", FileMode.Open) )

            {

                XmlReader reader = XmlReader.Create(fs);

               reader.MoveToElement();

                XmlSerializer xs =new XmlSerializer(typeof(Paper), "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27");

                Paper paper1 = (Paper)xs.Deserialize(reader);

            }

        }

    }

}

 

就此InfoPath打开我们序列化的Xml文件试试?喔……

 

实在,既然从Xml节点到路的映照这么直白,能免可知生出只自动化的道生成映射的类别也?呵呵,未完待续.

相关文章

网站地图xml地图