码迷,mamicode.com
首页 > 其他好文 > 详细

通过XmlDocument读写Xml文档参考地址

时间:2015-11-08 14:56:33      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
/// 获取一个报表的参数  http://blog.csdn.net/hdhai9451/article/details/12170069
/// </summary>
public static ReportAdapterSection GetReportAdapterSectionByID(string ReportID, ProfileRole RoleType, ReportTemplateType TemplateType)
{
    ReportAdapterSection reportModel = new ReportAdapterSection();
    XmlDocument xmlDoc = new XmlDocument();
    string configFile = GetReportConfigFile(RoleType, TemplateType);
    xmlDoc.Load(configFile);
    XmlNodeList nodes = xmlDoc.SelectSingleNode("ReportConfig").ChildNodes;
    List<ReportParamSection> list = new List<ReportParamSection>();

    foreach (XmlElement node in nodes)
    {
        if (node.HasChildNodes && node.Attributes["ID"].Value == ReportID)
        {
            string MainID = node.Attributes["ID"].Value;
            reportModel.ID = MainID;
            reportModel.Title = node.Attributes["Title"].Value;
            string UniqueKey = string.Empty;
            if (node.Attributes["UniqueKey"] != null)
            {
                UniqueKey = node.Attributes["UniqueKey"].Value;
            }
            reportModel.UniqueKey = UniqueKey;
            string SummaryAmtOrNum = string.Empty;
            if (node.Attributes["SummaryAmtOrNum"] != null)
            {
                SummaryAmtOrNum = node.Attributes["SummaryAmtOrNum"].Value;
            }
            reportModel.SummaryAmtOrNum = SummaryAmtOrNum;

            bool IsAddTotal = false;
            if (node.Attributes["IsAddTotal"] != null)
            {
                IsAddTotal = node.Attributes["IsAddTotal"].Value == "1" ? true : false;
            }
            reportModel.IsAddTotal = IsAddTotal;
            XmlNode paramsNode = node.SelectSingleNode("Params");
            if (paramsNode != null && paramsNode.HasChildNodes)
            {
                foreach (XmlElement item in paramsNode)
                {
                    ReportParamSection model = new ReportParamSection();
                    model.MainID = MainID;
                    model.Title = item.Attributes["ParaTitle"].Value;
                    model.Type = (ReportParaType)Enum.Parse(typeof(ReportParaType), item.Attributes["ParaType"].Value);

                    //参数
                    string paras1 = string.Empty, paras2 = string.Empty;
                    paras1 = item.Attributes["Para1Name"].Value;
                    if (item.Attributes["Para2Name"] != null)
                    {
                        paras2 = item.Attributes["Para2Name"].Value;
                    }
                    model.Params = new string[] { paras1, paras2 };
                    //默认值
                    string dvalue1 = string.Empty, dvalue2 = string.Empty;
                    if (item.Attributes["Default1Value"] != null)
                    {
                        dvalue1 = item.Attributes["Default1Value"].Value;
                    }
                    if (item.Attributes["Default2Value"] != null)
                    {
                        dvalue2 = item.Attributes["Default2Value"].Value;
                    }
                    model.DefaultValues = new string[] { dvalue1, dvalue2 };
                    //条件标题
                    string ConditionTitle = string.Empty;
                    if (item.Attributes["ConditionTitle"] != null)
                    {
                        ConditionTitle = item.Attributes["ConditionTitle"].Value;
                    }
                    model.ConditionTitle = ConditionTitle;
                    list.Add(model);
                }
            }
            XmlNode sqlNode = node.SelectSingleNode("SqlStatement");
            reportModel.MasterSqlStatement = sqlNode.InnerText;
            if (node.SelectSingleNode("Remark1") != null)
            {
                reportModel.Remark1 = node.SelectSingleNode("Remark1").Attributes["Text"].Value;
            }
            if (node.SelectSingleNode("Remark2") != null)
            {
                reportModel.Remark2 = node.SelectSingleNode("Remark2").Attributes["Text"].Value;
            }
            if (node.SelectSingleNode("Remark3") != null)
            {
                reportModel.Remark3 = node.SelectSingleNode("Remark3").Attributes["Text"].Value;
            }
        }
    }
    reportModel.ParamSettings = list;
    return reportModel;
}
<?xml version="1.0" encoding="utf-8" ?>
<ReportConfig>
  <Report ID="1" Title="The InKindItem Information">
    <Params>
      <ParamItem ParaType="DateRange" ParaTitle="饋贈日期" Para1Name="@StartDate" Para2Name="@EndDate"  Default1Value="-365" Default2Value="0" ConditionTitle="InKindItemDate"/>
    </Params>
    <SqlStatement>
      <![CDATA[
select * 
from ProfileInKindItem
where InKindDate between @StartDate and @EndDate

    
        ]]>
    </SqlStatement>
  </Report>
</ReportConfig>

 

http://www.cnblogs.com/yukaizhao/archive/2011/07/19/csharp_xmldocument_access_xml.html

<?xml version="1.0" encoding="utf-8" ?>
<students>
  <!--我是一段注释文字-->
  <student name="张平">
    <courses>
      <course name="语文?">
        <teacherComment>
          <![CDATA[
        这里是语文老师的批注
        ]]>
        </teacherComment>      
    </course>
 
      <course name="数学">
        <teacherComment>
          <![CDATA[
        这里是数学老师的批注
        ]]>
        </teacherComment>
      </course>
    </courses>
  </student>
</students>

1.如何使用XmlDocument读取Xml

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
 
namespace XmlExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlFilePath = @"X:\about.net\example\XmlExample\1.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFilePath);
 
            //使用xpath表达式选择文档中所有的student子节点
            XmlNodeList studentNodeList = doc.SelectNodes("/students/student");
            if (studentNodeList != null)
            {
                foreach (XmlNode studentNode in studentNodeList)
                {
                    //通过Attributes获得属性名字为name的属性
                    string name = studentNode.Attributes["name"].Value;
                    Console.WriteLine("Student:" + name);
 
                    //通过SelectSingleNode方法获得当前节点下的courses子节点
                    XmlNode coursesNode = studentNode.SelectSingleNode("courses");
 
                    //通过ChildNodes属性获得courseNode的所有一级子节点
                    XmlNodeList courseNodeList = coursesNode.ChildNodes;
                    if (courseNodeList != null)
                    {
                        foreach (XmlNode courseNode in courseNodeList)
                        {
                            Console.Write("\t");
                            Console.Write(courseNode.Attributes["name"].Value);
                            Console.Write("老师评语");
                            //通过FirstNode属性可以获得课程节点的第一个子节点,LastNode可以获得最后一个子节点
                            XmlNode teacherCommentNode = courseNode.FirstChild;
                            //读取CData节点
                            XmlCDataSection cdata = (XmlCDataSection)teacherCommentNode.FirstChild;
                            Console.WriteLine(cdata.InnerText.Trim());
                        }
                    }
                }
            }
 
            Console.Write("\r\nPress any key to continue....");
            Console.Read();
        }
    }
}

2.如何通过XmlDocument编辑Xml

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
 
namespace WriteXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //创建Xml声明部分,即<?xml version="1.0" encoding="utf-8" ?>
            xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
 
            //创建根节点
            XmlNode rootNode = xmlDoc.CreateElement("students");
 
            //创建student子节点
            XmlNode studentNode = xmlDoc.CreateElement("student");
            //创建一个属性
            XmlAttribute nameAttribute = xmlDoc.CreateAttribute("name");
            nameAttribute .Value = "张同学";
            //xml节点附件属性
            studentNode.Attributes.Append(nameAttribute);
 
            
            //创建courses子节点
            XmlNode coursesNode = xmlDoc.CreateElement("courses");
            XmlNode courseNode1 = xmlDoc.CreateElement("course");
            XmlAttribute courseNameAttr = xmlDoc.CreateAttribute("name");
            courseNameAttr.Value = "语文";
            courseNode1.Attributes.Append(courseNameAttr);
            XmlNode teacherCommentNode = xmlDoc.CreateElement("teacherComment");
            //创建Cdata块
            XmlCDataSection cdata = xmlDoc.CreateCDataSection("<font color=\"red\">这是语文老师的批注</font>");
            teacherCommentNode.AppendChild(cdata);
            courseNode1.AppendChild(teacherCommentNode);
            coursesNode.AppendChild(courseNode1);
            //附加子节点
            studentNode.AppendChild(coursesNode);
 
            rootNode.AppendChild(studentNode);
            //附加根节点
            xmlDoc.AppendChild(rootNode);
 
            //保存Xml文档
            xmlDoc.Save(@"d:\test.xml");
 
            Console.WriteLine("已保存Xml文档");
 
 
        }
    }
}

 

通过XmlDocument读写Xml文档参考地址

标签:

原文地址:http://www.cnblogs.com/shy1766IT/p/4947012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!