Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8259551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:53:28+00:00 2026-06-08T02:53:28+00:00

I have the following XML document that needs to be parsed: … <tx size_total=143>

  • 0

I have the following XML document that needs to be parsed:

...
<tx size_total="143">
  <type size="1" start_key="02">STX</type>
  <type size="3">Type</type>
  <type size="3" decimal="true">Serial</type>
  <type size="3" key="23 64 31">Function_Code</type>
  <type size="2" decimal="true">LIU</type>
  <type size="1">Status</type>
  <type size="2" repeat="64" binary ="true" binary_discard="2">Value</type>
  <type size="1">ETX</type>
  <type size="1">LRC</type>
...

I wrote the following code for parsing:

XmlNodeList typeNodeList = txNode.SelectNodes(TYPE_NODE);
CommRuleContainer rc = new CommRuleContainer(funcNode.Attributes.GetNamedItem("name").Value,
                        txNode.Attributes.GetNamedItem("size_total").Value, funcNode.Attributes.GetNamedItem("id").Value);
foreach (XmlNode tNode in typeNodeList)
{
    int size = Convert.ToInt32(tNode.Attributes.GetNamedItem("size").Value);
    int repeat = Convert.ToInt32(tNode.Attributes.GetNamedItem("repeat").Value);
    int binary_discard = Convert.ToInt32(tNode.Attributes.GetNamedItem("binary_discard").Value);
    string start_key = tNode.Attributes.GetNamedItem("start_key").Value;
    string key = tNode.Attributes.GetNamedItem("key").Value;
    bool convert_decimal = false, convert_binary = false;
    if (tNode.Attributes.GetNamedItem("decimal").Value == "true")
                                convert_decimal = true;
    if (tNode.Attributes.GetNamedItem("binary").Value == "true")
                                convert_binary = true;
    rc.AddTypeDefinition(tNode.Value, size, repeat, binary_discard, convert_decimal, convert_binary);
}

The code throws a nullreferenceexception if I try to obtain the value of a certian attribute that doesn’t exist (I.E: tNode.Attribute.GetNamedItem(“repeat”).value fails on all nodes that doesn’t have the repeat attribute). What is a way that I can verify if a certain attribute exists?

Also the above code isn’t clean at all. What is the best way to organize the above code?

Edit: I am aware of the approach where you can individually check whether the attributes are null or not before getting the values off them but this makes the code look very dirty as I am required to write a lot of ifs (or nested ifs)

if (tNode.Attributes.GetNamedItem("decimal") != null)
   if (tNode.Attributes.GetNamedItem("decimal").Value == "true")
       convert_decimal = true;

This becomes problematic in the long run if I have to write a lot more attributes. I’d like to know more of an organized approach for this (Perhaps XML Attributes can be enumerated? I don’t know.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:53:30+00:00Added an answer on June 8, 2026 at 2:53 am

    Agree with @nunespascal and here is the code I prepared for you already .. he answered quicker than me.. LOL:

    static void Main(string[] args)
            {
                var serialized = @"
    <tx size_total=""143""> 
      <type size=""1"" start_key=""02"">STX</type> 
      <type size=""3"">Type</type> 
      <type size=""3"" decimal=""true"">Serial</type> 
      <type size=""3"" key=""23 64 31"">Function_Code</type> 
      <type size=""2"" decimal=""true"">LIU</type> 
      <type size=""1"">Status</type> 
      <type size=""2"" repeat=""64"" binary =""true"" binary_discard=""2"">Value</type> 
      <type size=""1"">ETX</type> 
      <type size=""1"">LRC</type></tx>";
                var deserialized = serialized.XmlDeserialize<Tx>();
            }
        }
    
        [XmlRoot("tx")]
        public class Tx
        {
            [XmlAttribute("size_total")]
            public int TotalSize { get; set; }
    
            [XmlElement("type")]
            public List<TxType> Types { get; set; }
    
            public Tx()
            {
                Types = new List<TxType>();
            }
        }
    
        public class TxType
        {
            [XmlAttribute("size")]
            public string Size { get; set; }
    
            [XmlAttribute("decimal")]
            public bool IsDecimal { get; set; }
    
            [XmlAttribute("binary")]
            public bool IsBinary { get; set; }
    
            [XmlAttribute("start_key")]
            public string StartKey { get; set; }
    
            [XmlAttribute("key")]
            public string Key { get; set; }
    
            [XmlAttribute("repeat")]
            public int Repeat { get; set; }
    
            [XmlAttribute("binary_discard")]
            public int BinaryDiscard { get; set; }
    
            [XmlText]
            public string Value { get; set; }
        }
    

    here is my helper class for deserializing:

    public static class StringExtensions
        {
            /// <summary>
            /// Deserializes the XML data contained by the specified System.String
            /// </summary>
            /// <typeparam name="T">The type of System.Object to be deserialized</typeparam>
            /// <param name="s">The System.String containing XML data</param>
            /// <returns>The System.Object being deserialized.</returns>
            public static T XmlDeserialize<T>(this string s)
            {
                var locker = new object();
                var stringReader = new StringReader(s);
                var reader = new XmlTextReader(stringReader);
                try
                {
                    var xmlSerializer = new XmlSerializer(typeof(T));
                    lock (locker)
                    {
                        var item = (T)xmlSerializer.Deserialize(reader);
                        reader.Close();
                        return item;
                    }
                }
                catch
                {
                    return default(T);
                }
                finally
                {
                    reader.Close();
                }
            }
        }
    

    That should get you off to a good start. Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML Document (that can be redesigned if necessary) that stores
I have a filesyste that is represented in an xml document in the following
I have a simple xml document that looks like the following snippet. I need
I have the following XML Document: <?xml version=\1.0\ encoding=\UTF-8\?> <atom:entry xmlns:atom=\http://www.w3.org/2005/Atom\ xmlns:apps=\http://schemas.google.com/apps/2006\ xmlns:gd=\http://schemas.google.com/g/2005\> <apps:property
I have the following xml document, I need an xquery expression to know how
I have the following XML document: <tt xmlns=http://www.w3.org/ns/ttml xmlns:tts=http://www.w3.org/ns/ttml#styling xml:lang=en> <head></head> <body> <div xml:lang=it>
I have following CDATA inside xml document: <![CDATA[ <p xmlns=>Refer to the below: <br/>
I have an XML document with a section similar to the following: <release_list> <release>
I have an xml document which consists of a number of the following: -
I have an XML document which contains nodes like following:- <a class=custom>test</a> <a class=xyz></a>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.