XMLWriter: WriteStartElement with a tag name and string to indicate tag name

king jia picture king jia · May 8, 2013 · Viewed 10k times · Source

I have same tag names and different strings to different the tag name.

here is the XML.

<order>
  <ID>1001</ID> 
  <config>
    <properties>
      <entry key="Total">10</entry> 
      <entry key="Name">name</entry> 
      <entry key="Config">COMMON</entry> 
      <entry key="Delivery">15-FEBRUARY-2013</entry> 
      <entry key="Setting">name</entry> 
    </properties>
    <id>19</id> 
  </config>
  <aID>58239346</aID> 
</order>

here is my current code:

public String cards(string id)
    {
        StringWriter str = new StringWriter();
        XmlTextWriter xmlWriter = new XmlTextWriter(str);
        xmlWriter.Formatting = Formatting.Indented;
        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("order");
        xmlWriter.WriteElementString("ID", "1001");
        xmlWriter.WriteStartElement("config");
        xmlWriter.WriteStartElement("properties");
        /*
         * Create <entry key> at here 
         * 
         * 
         * 
         *
         */
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndElement();
        xmlWriter.WriteElementString("ClientID", id);
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndDocument();
        xmlWriter.Flush();
        xmlWriter.Close();
        return str.ToString();
    }

How to write the entry tag for XMLWriter??? I have no idea how to write it.

Answer

Marc Gravell picture Marc Gravell · May 8, 2013

The question seems to be about the <entry> tags; that is basically a series of 5 blocks similar to:

xw.WriteStartElement("entry");
xw.WriteAttributeString("key", "RecordTotal");
xw.WriteString("10");
xw.WriteEndElement();

However, you might also want to look at XmlSerializer - would probably make this a lot easier:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

static class Program {
    static void Main() {
        var order = new Order {
            ClientId = 1001,
            Id = 58239346,
            Config = new OrderConfig {
                Id = 19,
                Properties = {
                    new OrderProperty { Key = "RecordTotal", Value = "10"},
                    new OrderProperty { Key = "InputFileName", Value = "name"},
                    new OrderProperty { Key = "ConfigName", Value = "COMMON_"},
                    new OrderProperty { Key = "DeliveryDate", Value = "15-FEBRUARY-2013"},
                    new OrderProperty { Key = "Qualifier", Value = "name"}
                }
            }
        };
        var ser = new XmlSerializer(typeof(Order));
        ser.Serialize(Console.Out, order);
    }
}
[XmlRoot("order")]
public class Order {
    [XmlElement("clientID", Order = 0)]
    public int ClientId { get; set; }    
    [XmlElement("config", Order = 1)]
    public OrderConfig Config { get; set; }    
    [XmlElement("orderID", Order = 2)]
    public int Id { get; set; }
}

public class OrderConfig {
    [XmlElement("id", Order = 2)]
    public int Id { get; set; }    
    private readonly List<OrderProperty> properties = new List<OrderProperty>();
    [XmlArray("properties", Order = 1), XmlArrayItem("entry")]
    public List<OrderProperty> Properties { get { return properties; } }
}

public class OrderProperty {
    [XmlAttribute("key")]
    public string Key {get;set;}
    [XmlText]
    public string Value {get;set;}
}