Home > Blockchain >  How to read multiple node element from xml data in c#
How to read multiple node element from xml data in c#

Time:02-04

I have xml like below. Here allowedSessionType can be multiple value sometime. Could anyone help me to read both and store it into data table c# gridview.

I tried but getting single value

enter image description here

<sessionManagementSubscriptionData>
<singleNssai>1-000001</singleNssai>
<dnnConfiguration>
<pduSessionTypes>
<defaultSessionType>IPV4</defaultSessionType>
<allowedSessionType>IPV4</allowedSessionType>
</pduSessionTypes>
<sscModes>
<defaultSscMode>SSC_MODE_1</defaultSscMode>
<allowedSscMode>SSC_MODE_1</allowedSscMode>
</sscModes>
</dnnConfiguration>

<dnnConfiguration>
<pduSessionTypes>
<defaultSessionType>IPV4</defaultSessionType>
<allowedSessionType>IPV4</allowedSessionType>
<allowedSessionType>IPV6</allowedSessionType>
</pduSessionTypes>
<sscModes>
<defaultSscMode>SSC_MODE_1</defaultSscMode>
<allowedSscMode>SSC_MODE_1</allowedSscMode>
</sscModes>
</dnnConfiguration>
</sessionManagementSubscriptionData>

DataTable dt1 = new DataTable(); dt1.Columns.Add("Allowed Session Type", typeof(string));

XmlNodeList nodeList1 = doc.SelectNodes("//sessionManagementSubscriptionData/dnnConfiguration");

foreach (XmlNode node1 in nodeList1)
{
DataRow dtrow1 = dt1.NewRow();

var SMSDDefaultSessionType = node1.SelectSingleNode("//defaultSessionType").InnerText;
dtrow1["Default Session Type"] = SMSDDefaultSessionType;

var SMSDallowedSessionType = node1.SelectSingleNode("//allowedSessionType").InnerText;
dtrow1["Allowed Session Type"] = SMSDallowedSessionType;
dt1.Rows.Add(dtrow1);
}
GridView2.DataSource = dt1;
GridView2.DataBind();

CodePudding user response:

using System.Xml.Serialization;
XmlSerializer serializer = new XmlSerializer(typeof(SessionManagementSubscriptionData));
using (StringReader reader = new StringReader(xml))
{
   var test = (SessionManagementSubscriptionData)serializer.Deserialize(reader);
   //test.DnnConfiguration has your data
}
[XmlRoot(ElementName="pduSessionTypes")]
public class PduSessionTypes { 

    [XmlElement(ElementName="allowedSessionType")] 
    public List<string> AllowedSessionType; 
}

[XmlRoot(ElementName="sscModes")]
public class SscModes { 

    [XmlElement(ElementName="defaultSscMode")] 
    public string DefaultSscMode; 

    [XmlElement(ElementName="allowedSscMode")] 
    public string AllowedSscMode; 
}

[XmlRoot(ElementName="dnnConfiguration")]
public class DnnConfiguration { 

    [XmlElement(ElementName="pduSessionTypes")] 
    public PduSessionTypes PduSessionTypes; 

    [XmlElement(ElementName="sscModes")] 
    public SscModes SscModes; 
}

[XmlRoot(ElementName="sessionManagementSubscriptionData")]
public class SessionManagementSubscriptionData { 

    [XmlElement(ElementName="singleNssai")] 
    public DateTime SingleNssai; 

    [XmlElement(ElementName="dnnConfiguration")] 
    public List<DnnConfiguration> DnnConfiguration; 
}

Use this webiste to convert xml to c#

CodePudding user response:

Here is results in a DataTable using Xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication11
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Default Session Type", typeof(string));
            dt.Columns.Add("Allowed Session Type", typeof(string));
            dt.Columns.Add("Default SSC Mode", typeof(string));
            dt.Columns.Add("Allowed SSC Mode", typeof(string));

            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement dnnConfiguration in doc.Descendants("dnnConfiguration"))
            {
                string defaultSessionType = (string)dnnConfiguration.Descendants("defaultSessionType").FirstOrDefault();
                string[] allowedSessionType = dnnConfiguration.Descendants("allowedSessionType").Select(x => (string)x).ToArray();
                string defaultSscMode = (string)dnnConfiguration.Descendants("defaultSscMode").FirstOrDefault();
                string[] allowedSscMode = dnnConfiguration.Descendants("allowedSscMode").Select(x => (string)x).ToArray();

                dt.Rows.Add(new object[] { defaultSessionType, string.Join(",", allowedSessionType), defaultSscMode, string.Join(",", allowedSscMode) });

            }

        }
  
    }
 
}
  •  Tags:  
  • Related