简体   繁体   中英

XMLReader read attributes

I'm serializing a dictionary to xml which works fine and generates each item as follows:

  <Parameters>
    <item key="Input" value="CLOCK_SYNC" />
    <item key="Output" value="NTP_SYNC" />
    <item key="TimeSpan" value="00:00:30" />
  </Parameters>

However trying to deserialize this is giving me some headache with the xml reader, im trying to loop through the item elements and read the attributes back, but i have not been able to do so wiht the following code. I have tried various ways to iterate through, but in neither case succesful, im close to just parse character by character and make it work that way but im sure im overlooking something

    public void ReadXml(System.Xml.XmlReader reader)
    {
        bool wasEmpty = reader.IsEmptyElement;
        reader.MoveToContent();

        if (wasEmpty)
            return;

        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            reader.MoveToAttribute("key");

            string key = reader.GetAttribute("key");

            reader.MoveToAttribute("value");

            string value = reader.GetAttribute("value");

            this.Add(key, value);

            reader.MoveToContent();
        }

It errors out as i cant parse the attributes, they both return Null.

Fixed it now with a lot of extra reads, but it works out, still confused about the ReadStartElement and MoveToContent but oh well.

    public void ReadXml(System.Xml.XmlReader reader)
    {
        bool wasEmpty = reader.IsEmptyElement;
        // jump to <parameters>
        reader.Read();

        if (wasEmpty)
            return;

        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            // jump to <item>
            reader.MoveToContent();

            reader.MoveToAttribute("key");

            string key = reader.GetAttribute("key");

            reader.MoveToAttribute("value");

            string value = reader.GetAttribute("value");

            this.Add(key, value);

            reader.ReadStartElement("item");

            reader.MoveToContent();
        }
    }

XMLReader is very particular about order in which you work through an element. You enter an element, read it's value/content, then read its attributes.

Also, not having an end element also makes XMLReader angry, because you can't do this:

reader.ReadStartElement("item")
'Do work
reader.ReadEndElement()

The below example is how you would navigate through a particular element with no value/content.

reader.ReadStartElement("Parameters")
While reader.NodeType <> System.Xml.XmlNodeType.EndElement
    Select Case reader.NodeType
        Case Xml.XmlNodeType.Element
            If reader.Name <> "item" Then
                reader.ReadToNextSibling("item")
                Continue While
            End If

            Dim key As TKey
            Dim value As TValue
            If (reader.HasAttributes) Then
                reader.MoveToFirstAttribute()
                key = Convert.ChangeType(reader.GetAttribute("key"), GetType(TKey))
                value = Convert.ChangeType(reader.GetAttribute("value"), GetType(TValue))
            End If

            Me.Add(key, value)
            reader.MoveToContent()
        Case Else
            reader.ReadToNextSibling("item")
            Continue While
    End Select
End While
reader.ReadEndElement()

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