简体   繁体   中英

Best way to parse XML Attributes

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 (IE: 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.)

if(null != tNode.Attributes.GetNamedItem("repeat"))
   repeat = Convert.ToInt32(tNode.Attributes.GetNamedItem("repeat").Value);

Update

Now that you know not to get a null reference,

The best solution to this would be to write a class into which you would deserialize your xml using the XmlSerializer .

This article on custom serialization will get you started.

To use XML serialization, you must first mark up your data objects with attributes that indicate the desired XML mapping. These attributes are found in the System.Xml.Serialization namespace and include the following:

XmlRoot Specifies the name of the root element of the XML file. By default, XmlSerializer will use the name of the class. This attribute can be applied to the class declaration.

  • XmlElementIndicates the element name to use for a property or public variable. By default, XmlSerializer will use the name of the property or public variable.
  • XmlAttributeIndicates that a property or public variable should be serialized as an attribute, not an element, and specifies the attribute name.
  • XmlEnumConfigures the text that should be used when serializing enumerated values. If you don't use XmlEnum, the name of the enumerated constant will be used.
  • XmlIgnoreIndicates that a property or public variable should not be serialized.

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.

Well, here is an entirely untested mashup-function, YMMV.

static class XmlNodeExtensions {
    public static T GetAttrValue(this XmlNode node, string attrName) {
        return GetAttrValue(node, attrName, default(T));
    }
    public static T GetAttrValue(this XmlNode node, string attrName, T defaultValue) {
        var attr = node.Attributes.GetNamedItem(attrName);
        if (attr != null) {
            var value = attr.Value; 
            var tT = typeof(T);       // target Type
            if (tT.IsAssignableFrom(typeof(string)))
            {
                return (T)value;
            } else
            {
                var converter = TypeDescriptor.GetConverter(tT);
                if (converter.CanConvertFrom(typeof(string)))
                {
                    return (T)converter.ConvertFrom(value);
                } else
                {
                    throw new InvalidOperationException("No conversion possible");
                }
            }
        } else {
            return defaultValue;
        }
    }
}

.. then ..

var id = node.GetAttrValue<int>("size");
var name = node.GetAttrValue("name", "no name!");
var titleOrNull = node.GetAttrValue<string>("title");

.. or something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM