How to search for specific data on the same line?

Asked

Viewed 52 times

4

I’m reading a file XML where I have to take the values of certain data that are on the same line, example:

I have the following line:

<"Row GenericKitName=\"12000\" QueryID=\"999\" SubQueryID=\"0\" OutboundMessageCode=\"100\" OutboundMessageType=\"CannedMessage\" Description=\"Confirma Panico\" Prompt=\"\" IsHexadecimalConvert=\"False\" Data=\"0\" Name=\"Confirma Panico\" Priority=\"0\" />

How I can search for each item and assign its value?

Example:

  • Catch Generickitname and the value of 12000 and play on an object.

  • Catch the Queryid and the value of 999 and play on an object


static void Main(string[] args)
{  

    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(@"C:\Users\igor.carreiro\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2\XML\Bitologia_19_novembro_2018_NCC.xml");

    XmlNodeList listEnvio = xmldoc.SelectNodes("/TICM/DataProvider");
    Foward foward = new Foward();
    foreach (XmlNode row in listEnvio)
    {
        XmlNode envio = row.SelectSingleNode("FCM");

        if (envio != null)
        {
            string[] linhas = null;
            string linha = envio.FirstChild.OuterXml.ToString();
            linhas = linha.Split('<');
            List<Foward> listaEnvio = new List<Foward>();

            for (int i = 0; i < linhas.Length; i++)
            {
                foward.GenericKitName = //algo aqui;
                foward.QueryID = //algo aqui;
            }

        }
    }
    Console.ReadKey();
}

Arquvio XML:

<?xml version="1.0" encoding="utf-8"?>
<TICM>
  <DataProvider>
    <FCM>
      <Row GenericKitName="12000" QueryID="999" SubQueryID="0" OutboundMessageCode="100" OutboundMessageType="CannedMessage" Description="Confirma Panico" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Confirma Panico" Priority="0" />
      <Row GenericKitName="12000" QueryID="66" SubQueryID="1" OutboundMessageCode="150" OutboundMessageType="FreeTextMessage" Description="Programa reconexão GPS OK" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Programa reconexão OK" Priority="0" Format="159,8,1,2,167,0,0,0" />
      <Row GenericKitName="12000" QueryID="66" SubQueryID="2" OutboundMessageCode="150" OutboundMessageType="FreeTextMessage" Description="Programa violação GPRS OK" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Programa violação OK" Priority="0" Format="159,7,1,2,168,0,0,0" />
      <Row GenericKitName="4000" QueryID="67" SubQueryID="0" OutboundMessageCode="43" OutboundMessageType="CannedMessage" Description="Desativa sinais SMART" Prompt="" IsHexadecimalConvert="False" Data="3768" Name="Desativa sinais SMART" Priority="0" />
      <Row GenericKitName="5000" QueryID="1" SubQueryID="0" OutboundMessageCode="200" OutboundMessageType="CannedMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="615" Name="Alarm status request" Priority="0" />
      <Row GenericKitName="5000" QueryID="2" SubQueryID="0" OutboundMessageCode="200" OutboundMessageType="CannedMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="2816" Name="History status request" Priority="0" />
      <Row GenericKitName="5000" QueryID="999" SubQueryID="0" OutboundMessageCode="100" OutboundMessageType="CannedMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Confirmacao de Panico" Priority="0" />
      <Row GenericKitName="5000" QueryID="3" SubQueryID="0" OutboundMessageCode="43" OutboundMessageType="CannedMessage" Description="Ativa Panico" Prompt="" IsHexadecimalConvert="False" Data="688" Name="Tulip Ativar Panico" Priority="0" />
      <Row GenericKitName="11000" QueryID="27" SubQueryID="0" OutboundMessageCode="94" OutboundMessageType="CannedMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="3371" Name="Bloquear SGPRS" Priority="0" />
      <Row GenericKitName="11000" QueryID="28" SubQueryID="0" OutboundMessageCode="94" OutboundMessageType="CannedMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="3339" Name="Desbloquear SGPRS" Priority="0" />
      <Row GenericKitName="12000" QueryID="31" SubQueryID="0" OutboundMessageCode="150" OutboundMessageType="FreeTextMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Ativa Panico EGPRS" Priority="0" Format="159,9,2,2,235,60,0,60" />
      <Row GenericKitName="12000" QueryID="32" SubQueryID="0" OutboundMessageCode="150" OutboundMessageType="FreeTextMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Desativa Panico EGPRS" Priority="0" Format="159,9,0,2,0,0,0,0" />
      <Row GenericKitName="12000" QueryID="33" SubQueryID="0" OutboundMessageCode="150" OutboundMessageType="FreeTextMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Programa 12006 e afins" Priority="0" Format="159,11,1,2,40,300,0,0" />
      <Row GenericKitName="12000" QueryID="34" SubQueryID="0" OutboundMessageCode="150" OutboundMessageType="FreeTextMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Ativa modo EVENTO REAL EGPRS" Priority="0" Format="159,11,1,2,40,20,0,0" />
      <Row GenericKitName="12000" QueryID="35" SubQueryID="0" OutboundMessageCode="150" OutboundMessageType="FreeTextMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Desativa modo EVENTO REAL EGPRS" Priority="0" Format="159,11,1,2,40,300,0,0" />
      <Row GenericKitName="12000" QueryID="36" SubQueryID="0" OutboundMessageCode="150" OutboundMessageType="FreeTextMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Programa 12004" Priority="0" Format="159,11,1,60,0,0,0,0" />
      <Row GenericKitName="12000" QueryID="39" SubQueryID="0" OutboundMessageCode="150" OutboundMessageType="FreeTextMessage" Description="" Prompt="" IsHexadecimalConvert="False" Data="0" Name="Programam EGPRS 1 minuto" Priority="0" Format="159,11,1,2,40,60,0,0" />
     </FCM>
   </DataProvider>
</TICM>
  • The biggest problem that not only this XML, I believe have more values there, you could put as it is the complete layout?

  • 1

    @Virgilionovic added!

2 answers

3


Create a class to serve as a template to store the items in this XML, example:

public class Rows
{
    public int GenericKitName { get; set; }
    public int QueryID { get; set; }
}

and the code to be followed to fill in the XML to a list of rows interacting on each item and adding to the typed list created with the two information template GenericKitName and QueryID:

List<Rows> RowsItems = new List<Rows>();

XmlDocument xml = new XmlDocument();
xml.Load("./data.xml");

XmlNodeList xmlNodeList = xml.DocumentElement.SelectNodes("/TICM/DataProvider/FCM/Row");
foreach (XmlNode item in xmlNodeList)
{
    var attr = item.Attributes;
    var GenericKitName = Convert.ToInt32(attr.GetNamedItem("GenericKitName").Value);
    var QueryID = Convert.ToInt32(attr.GetNamedItem("QueryID").Value);
    RowsItems.Add(new Rows { GenericKitName = GenericKitName, QueryID = QueryID });
}

That one XML in particular the values are in attributes so in the code within the foreach the property is used Attributes, giving access to all attributes of each row.

2

Using System.Xml.Linq.XDocument:

    var xml = @"<?xml version=""1.0"" encoding=""utf-16""?>
<TICM>
  <DataProvider>
    <FCM>
      <Row GenericKitName=""12000"" QueryID=""999"" SubQueryID=""0"" OutboundMessageCode=""100"" OutboundMessageType=""CannedMessage"" Description=""Confirma Panico"" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Confirma Panico"" Priority=""0"" />
      <Row GenericKitName=""12000"" QueryID=""66"" SubQueryID=""1"" OutboundMessageCode=""150"" OutboundMessageType=""FreeTextMessage"" Description=""Programa reconexão GPS OK"" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Programa reconexão OK"" Priority=""0"" Format=""159,8,1,2,167,0,0,0"" />
      <Row GenericKitName=""12000"" QueryID=""66"" SubQueryID=""2"" OutboundMessageCode=""150"" OutboundMessageType=""FreeTextMessage"" Description=""Programa violação GPRS OK"" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Programa violação OK"" Priority=""0"" Format=""159,7,1,2,168,0,0,0"" />
      <Row GenericKitName=""4000"" QueryID=""67"" SubQueryID=""0"" OutboundMessageCode=""43"" OutboundMessageType=""CannedMessage"" Description=""Desativa sinais SMART"" Prompt="""" IsHexadecimalConvert=""False"" Data=""3768"" Name=""Desativa sinais SMART"" Priority=""0"" />
      <Row GenericKitName=""5000"" QueryID=""1"" SubQueryID=""0"" OutboundMessageCode=""200"" OutboundMessageType=""CannedMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""615"" Name=""Alarm status request"" Priority=""0"" />
      <Row GenericKitName=""5000"" QueryID=""2"" SubQueryID=""0"" OutboundMessageCode=""200"" OutboundMessageType=""CannedMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""2816"" Name=""History status request"" Priority=""0"" />
      <Row GenericKitName=""5000"" QueryID=""999"" SubQueryID=""0"" OutboundMessageCode=""100"" OutboundMessageType=""CannedMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Confirmacao de Panico"" Priority=""0"" />
      <Row GenericKitName=""5000"" QueryID=""3"" SubQueryID=""0"" OutboundMessageCode=""43"" OutboundMessageType=""CannedMessage"" Description=""Ativa Panico"" Prompt="""" IsHexadecimalConvert=""False"" Data=""688"" Name=""Tulip Ativar Panico"" Priority=""0"" />
      <Row GenericKitName=""11000"" QueryID=""27"" SubQueryID=""0"" OutboundMessageCode=""94"" OutboundMessageType=""CannedMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""3371"" Name=""Bloquear SGPRS"" Priority=""0"" />
      <Row GenericKitName=""11000"" QueryID=""28"" SubQueryID=""0"" OutboundMessageCode=""94"" OutboundMessageType=""CannedMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""3339"" Name=""Desbloquear SGPRS"" Priority=""0"" />
      <Row GenericKitName=""12000"" QueryID=""31"" SubQueryID=""0"" OutboundMessageCode=""150"" OutboundMessageType=""FreeTextMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Ativa Panico EGPRS"" Priority=""0"" Format=""159,9,2,2,235,60,0,60"" />
      <Row GenericKitName=""12000"" QueryID=""32"" SubQueryID=""0"" OutboundMessageCode=""150"" OutboundMessageType=""FreeTextMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Desativa Panico EGPRS"" Priority=""0"" Format=""159,9,0,2,0,0,0,0"" />
      <Row GenericKitName=""12000"" QueryID=""33"" SubQueryID=""0"" OutboundMessageCode=""150"" OutboundMessageType=""FreeTextMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Programa 12006 e afins"" Priority=""0"" Format=""159,11,1,2,40,300,0,0"" />
      <Row GenericKitName=""12000"" QueryID=""34"" SubQueryID=""0"" OutboundMessageCode=""150"" OutboundMessageType=""FreeTextMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Ativa modo EVENTO REAL EGPRS"" Priority=""0"" Format=""159,11,1,2,40,20,0,0"" />
      <Row GenericKitName=""12000"" QueryID=""35"" SubQueryID=""0"" OutboundMessageCode=""150"" OutboundMessageType=""FreeTextMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Desativa modo EVENTO REAL EGPRS"" Priority=""0"" Format=""159,11,1,2,40,300,0,0"" />
      <Row GenericKitName=""12000"" QueryID=""36"" SubQueryID=""0"" OutboundMessageCode=""150"" OutboundMessageType=""FreeTextMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Programa 12004"" Priority=""0"" Format=""159,11,1,60,0,0,0,0"" />
      <Row GenericKitName=""12000"" QueryID=""39"" SubQueryID=""0"" OutboundMessageCode=""150"" OutboundMessageType=""FreeTextMessage"" Description="""" Prompt="""" IsHexadecimalConvert=""False"" Data=""0"" Name=""Programam EGPRS 1 minuto"" Priority=""0"" Format=""159,11,1,2,40,60,0,0"" />
     </FCM>
   </DataProvider>
</TICM>";

    var doc = XDocument.Parse(xml);
    var data = from element in doc.Descendants("Row")
               select new 
               {
                   GenericKitName = Convert.ToInt32((string)element.Attribute("GenericKitName")),
                   QueryID = Convert.ToInt32((string)element.Attribute("QueryID"))
               };

    foreach (var item in data)
    {
        Console.WriteLine(
            string.Format("GenericKitName: {0}, QueryID: {1}", item.GenericKitName, item.QueryID));
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.