linq 把 xml的值 赋值给其他? linq to xml 如何赋值? xml里面的特殊符号转义,asp.net word域合并 Aspose.Words 在线生成word

1:xml里面的特殊符号的转义

&  要换成  &

 

2:xml如下。

image

在后台,我们为了传值方便,做2个类。

一个是文件类 ,因为公司里面的文件是不一定的,可能有多个,之前是准备用 file1,file2,但是这样如果更多的话,就不好修改了

public class needtocreate  //这里是保存需要制作的文件,例如有的是需要制作4个,有的可能是6个,所以做成另外一个类别

    {

        public string file { get; set; }

    }
一个是公司类,用于保存 公司的信息和 文件的 列表,注意,里面有一个 List类型,是 文件类的列表
 
public class newcompany         //公司类,包括标题,文件夹,需要制作的文件列表,数据库

    {

        public string title { get; set; }

        public string folder { get; set; }

        public List<needtocreate> nt { get; set; }   // 注意这里的列表要加一个 public 不然下面就不能赋值了

        public string mdb { get; set; }

    }
我们根据 type id =”1” 这样来判断是什么公司类型,1就是1股东1
using System.Xml;

using System.Xml.Linq;
//通过 XDocument 这个 xml linq 对象来加载指定的xml  ,注意 ,这个是通过linq来实现的  一般的xml 是  XmlDocument
XDocument xml = XDocument.Load(Server.MapPath("newCompanyConfig.xml"));
 
// 使用查询语法获取指定的type集合

 var types = from t in xml.Root.Elements("type")

                                where t.Attribute("id").Value == type

                                select new

                                {

                                    title = t.Element("title").Value,

                                    folder = t.Element("folder").Value,

                                    mdb = t.Element("mdb").Value,

                                    xo = t.Elements("needtocreate")  //xo是指 needtocreate下的所有的子元素的集合,是元素,不是值

                                };
Response.Write(types.First().title);

如果我们在查询的时候,不是用的  select new  而是 select     t 的话,下面的获取值,就要修改一下

// 使用查询语法获取指定的type集合

                    var types = from t in xml.Root.Elements("type")

                                where t.Attribute("id").Value == "1"

                                select t;
Response.Write(types.First().Element("title").Value);

在获取needtocreate 的时候,很郁闷啊

 

List<needtocreate> need = new List<needtocreate> { };



                        //第一种方法 foreach

                        foreach (var item in types.First().xo.ElementAtOrDefault(0).Elements("file"))

                        {

                            //Response.Write(item.Value+"<br>");

                            need.Add(new needtocreate { file = item.Value });

                        }



                        //第二种方法,用for循环

                        int count = types.First().xo.ElementAtOrDefault(0).Elements("file").Count();

                        for (int i = 0; i < count; i++)

                        {

                            Response.Write(
types.First().xo.ElementAtOrDefault(0).Elements("file").Nodes().ToArray()[i].ToString() + "<br>");

                            need.Add(
new needtocreate { file = types.First().xo.ElementAtOrDefault(0).Elements("file").Nodes().ToArray()[i].ToString() });

                        }



                       

                        newcompany nc = new newcompany

                        {

                            title = types.First().title,

                            folder = types.First().folder,

                            mdb = types.First().mdb,

                              nt = need

                        };



                        //传入公司类,开始在线生产word文档

                         CreateDoc(nc);

最后,我们来生成word文档。

全部代码如下

/*********************************************************

 * 创建时间:2012/5/7 9:52:34

 * 描述说明:createdoc-newcompany   

 * 

 * 更改历史:

 * 

 * *******************************************************/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Aspose.Words;  //把Aspose.Words.dll 放在bin 目录下 ,记得在项目上引用一下

using System.Data;

using System.Data.OleDb;  //因为我用的是 access  

using System.Xml;

using System.Xml.Linq;



namespace asp.net_生成word_Aspose.Words

{

    



    public class needtocreate  //这里是保存需要制作的文件,例如有的是需要制作4个,有的可能是6个,所以做成另外一个类别

    {

        public string file { get; set; }

    }



    public class newcompany         //公司类,包括标题,文件夹,需要制作的文件列表,数据库

    {

        public string title { get; set; }

        public string folder { get; set; }

        public List<needtocreate> nt { get; set; }   // 注意这里的列表要加一个 public 不然下面就不能赋值了

        public string mdb { get; set; }

    }

    public partial class createdoc_newcompany : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            //首次加载需要执行的

            if (!Page.IsPostBack)

            {

                string type = Request.QueryString["type"];

                if (!string.IsNullOrEmpty(type))

                {

                    //通过 XDocument 这个 xml linq 对象来加载指定的xml  ,注意 ,这个是通过linq来实现的  一般的xml 是  XmlDocument

                    XDocument xml = XDocument.Load(Server.MapPath("newCompanyConfig.xml"));





                    // 使用查询语法获取指定的type集合

                    var types = from t in xml.Root.Elements("type")

                                where t.Attribute("id").Value == type

                                select new

                                {

                                    title = t.Element("title").Value,

                                    folder = t.Element("folder").Value,

                                    mdb = t.Element("mdb").Value,

                                    xo = t.Elements("needtocreate")  //xo是指 needtocreate下的所有的子元素的集合,是元素,不是值

                                };

                   

                    //如果查询xml不为空,那么就开始制作文件

                    if (types != null)

                    {

                         

                        List<needtocreate> need = new List<needtocreate> { };



                        //第一种方法 foreach

                        foreach (var item in types.First().xo.ElementAtOrDefault(0).Elements("file"))

                        {

                            //Response.Write(item.Value+"<br>");

                            need.Add(new needtocreate { file = item.Value });

                        }



                        ////第二种方法,用for循环

                        //int count = types.First().xo.ElementAtOrDefault(0).Elements("file").Count();

                        //for (int i = 0; i < count; i++)

                        //{

                        //    Response.Write(types.First().xo.ElementAtOrDefault(0).Elements("file").Nodes().ToArray()[i].ToString() + "<br>");

                        //    need.Add(new needtocreate { file = types.First().xo.ElementAtOrDefault(0).Elements("file").Nodes().ToArray()[i].ToString() });

                        //}



                       

                        newcompany nc = new newcompany

                        {

                            title = types.First().title,

                            folder = types.First().folder,

                            mdb = types.First().mdb,

                              nt = need

                        };



                        //传入公司类,开始在线生产word文档

                         CreateDoc(nc);



                    }

      

                }

            }

        }





        protected void CreateDoc(newcompany nc)

        {

            //循环判断这个公司有多少个word文件,

            foreach (var wordfile in nc.nt)

            {

                Response.Write(DateTime.Now.ToString());

                string tempZhuanMa = HttpUtility.HtmlDecode(wordfile.file);

                string temDocPath = Server.MapPath("~/word模板/" + nc.folder + "/" + tempZhuanMa);

                Aspose.Words.Document doc = new Document(temDocPath);



                //获取doc里面的 域名字

                var all = doc.MailMerge.GetFieldNames();



                //显示出来



                //foreach (var item in all)

                //{

                //    Response.Write(item.ToString() + "<br>");

                //}



                //建立一个和 access 的链接

                OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/数据库/" + nc.mdb));

                conn.Open();

                OleDbCommand cmd = new OleDbCommand("select * from list", conn);



                OleDbDataReader dr = cmd.ExecuteReader();

                string fileName = "";







                while (dr.Read())

                {

                    fileName = dr["coen"].ToString();

                }



                dr.Close();

                dr = cmd.ExecuteReader();





                string []temp1 = wordfile.file.Split('.');

                string temp2 = temp1[0].ToString();





                string date = DateTime.Now.ToString("yyyy-MM-dd-HH.mm.ss");



                doc.MailMerge.Execute(dr);

                string fileTitle = "/生成的word/" + fileName + date + ".doc";

                fileName = Server.MapPath("~/生成的word/" + temp2 +"_"+  fileName + "_" + date + ".doc");

                //fileName = "c:/joey.doc";   //如果用的是 vs2010的调试工具进行测试,居然可以保存到C盘

                doc.Save(fileName);  //保存到文件夹指定的地方 存为doc



                Response.Write("<a href='" + fileTitle + "' target='_blank'>点击下载</a><br>");

                //doc.Save(Response, "out.doc".ToString(), ContentDisposition.Attachment, null);  //保存为doc,浏览器直接提示下载

                conn.Close();

            }

            

        }

    }

}

你可能感兴趣的:(asp.net)