C#XMLコンテンツの読み込み

5818 ワード

/// <summary>
///  
/// </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ファイルの内容
<?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>