locked
Read XML from Sqlserve 2005 XML Data type for C# RRS feed

  • Question

  • User-1171089525 posted

    Hi All,

    this is my xml data which is stored in Sql table.

     <TestDQC>
      <Rule Id="9">
        <GenericParameters>
          <Params ParamName="CheckPrimaryCode" ParamValue="PrinDXCd" ParamType="Inline" />
          <Params ParamName="CheckSecCode_Prefix" ParamValue="SecDXCd_" ParamType="Inline" />
          <Params ParamName="CheckSecCode_Start" ParamValue="1" ParamType="Inline" />
          <Params ParamName="CheckSecCode_End" ParamValue="40" ParamType="Inline" />
          <Params ParamName="RollingMonth" ParamValue="12" ParamType="Inline" />
        </GenericParameters>
        <Condition Order="1">
          <RuleItem RuleDsc="DX1-40" ExecuteSourceType="SP" ExecuteSourceName="Usp_Audit_DQC_9" RuleType="SD" PluesOrMinus="2" />
        </Condition>
        <Condition Order="2" />
      </Rule>
    </TestDQC>

     

    I want to read this XML from my C# code

     

    Can you please help me?

    Regards,

    R.Vetrivelan

    Thursday, June 13, 2013 1:55 AM

All replies

  • User-1972223072 posted

    I did and tested, follow my code.

     void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
    
                var xml = XElement.Parse(@"<TestDQC>  <Rule Id='9'>    <GenericParameters>     
                                    <Params ParamName='CheckPrimaryCode' ParamValue='PrinDXCd' ParamType='Inline' />     
                                    <Params ParamName='CheckSecCode_Prefix' ParamValue='SecDXCd_' ParamType='Inline' />      
                                    <Params ParamName='CheckSecCode_Start' ParamValue='1' ParamType='Inline' />      
                                    <Params ParamName='CheckSecCode_End' ParamValue='40' ParamType='Inline' />     
                                    <Params ParamName='RollingMonth' ParamValue='12' ParamType='Inline' />    
                                    </GenericParameters>    <Condition Order='1'>      
                                    <RuleItem RuleDsc='DX1-40' ExecuteSourceType='SP' ExecuteSourceName='Usp_Audit_DQC_9' 
                                    RuleType='SD' PluesOrMinus='2' />    </Condition>   </Rule></TestDQC>");
    
                var rules = new List<Rule>();
    
                rules.AddRange(xml.Elements("Rule").Select(ele => new Rule{
                        RuleId = ele.Attribute("Id").Value,
                        Params = GetParams(ele.Element("GenericParameters")),
                        Conditions = GetConditions(ele)
    }
    
      private List<Condition> GetConditions(XElement xElement)
            {
                var conditions = new List<Condition>();
    
                conditions.AddRange(xElement.Elements("Condition").Select(ele => new Condition
                {
                    Order = ele.Attribute("Order").Value,
                    Rules = GetRules(ele)
                }));
    
                return conditions;
            }
    
            private List<Parameter> GetParams(XElement xElement)
            {  
    
                var parameters = new List<Parameter>();
    
                parameters.AddRange(xElement.Elements().Select(ele => new Parameter
                {
                    ParamName = ele.Attribute("ParamName").Value,
                    ParamValue = ele.Attribute("ParamValue").Value,
                    ParamType = ele.Attribute("ParamType").Value,
                }));
    
    
                return parameters;
            }
    
            private List<RuleItem> GetRules(XElement xElement)
            {
                var ruleItems = new List<RuleItem>();
    
                ruleItems.AddRange(xElement.Elements("RuleItem").Select(ele => new RuleItem
                {
                    RuleDsc = ele.Attribute("RuleDsc").Value,
                    ExecuteSourceType = ele.Attribute("ExecuteSourceType").Value,
                    ExecuteSourceName = ele.Attribute("ExecuteSourceName").Value,
                    RuleType = ele.Attribute("RuleType").Value,
                    PluesOrMinus = ele.Attribute("PluesOrMinus").Value,
                    }));
    
                return ruleItems;
            }
    
    
     public class Rule
        {
            public string RuleId { get; set; }
            public List<Parameter> Params { get; set; }
            public List<Condition> Conditions { get; set; }
           
        }
    
        public class Parameter
        {
            public string ParamName { get; set; }
            public string ParamValue { get; set; }
            public string ParamType { get; set; }
        }
    
    
        public class Condition
        {
            public string Order { get; set; }
            public List<RuleItem> Rules { get; set; }
        }
    
        public class RuleItem 
        {
            public string RuleDsc { get; set; }
            public string ExecuteSourceType { get; set; }
            public string ExecuteSourceName { get; set; }
            public string RuleType { get; set; }
            public string PluesOrMinus { get; set; }
        }



    Thursday, June 13, 2013 5:47 AM
  • User-1171089525 posted

    Thank you very much Syed..

    But I am using .net version 2.0 , there I can not use Linq

     

    Please suggesst.

    Regards,

    R.Vetrivelan.

     

     

    Monday, June 17, 2013 7:48 AM