2013-05-08 71 views
2

我具有相同的標記名稱和不同的字符串以標記名稱不同。XMLWriter:WriteStartElement帶有標記名稱和字符串以指示標記名稱

這裏是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> 

這裏是我當前的代碼:

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(); 
    } 

如何寫XMLWriter的入境標籤???我不知道如何寫它。

回答

6

這個問題似乎是關於<entry>標籤;這基本上是一系列類似的5塊:

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

然而,你可能也想看看XmlSerializer - 很可能使這個很多簡單:

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;} 
} 
+0

@kingjia我添加了一個完整的'XmlSerializer'示例,FYI – 2013-05-08 08:25:39

1

試試這個每個項:

xmlWriter.WriteStartElement("entry"); 
xmlWriter.WriteAttributeString("key", "RecordTotal"); 
xmlWriter.WriteValue(10); 
xmlWriter.WriteEndElement(); 
0

你可以試試下面的新條目:

xmlWriter.WriteStartElement("entry"); 
xmlWriter.WriteAttributeString("key", "RecordTotal"); 
xmlWriter.WriteValue(10); 
xmlWriter.WriteEndElement(); 

您可能想看看XML Serizalizer。下面是關於一個代碼示例如下:

 try 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(T)); 
      StringWriter writer = new StringWriter(); 
      serializer.Serialize(writer, myClass); 

      StringBuilder sb = new StringBuilder(writer.ToString()); 
              return sb.ToString(); 
     } 
     catch (Exception ex) 
     { 
      throw new System.Exception("Class To XML Error: " + ex.Message); 
     } 
+0

您的'XML_HEADER_FILTER'在那裏試圖做什麼?如果這是刪除多餘的名稱空間聲明,你可以要求它不包含它們... – 2013-05-08 08:26:31

+0

@MarcGravell我已經刪除了XML_HEADER_FILTER行代碼 - 這仍然沒有問題。我已經從我自己的代碼片斷中保存了這些信息,並表示道歉。 – Sean 2013-05-08 08:29:11

+0

但是對於您的信息 - 如果要刪除'xmlns'條目,則可以指示'XmlSerializer'來省略它們;如果你看看我在這個問題上的例子,但將最後一行改爲:var ns = new XmlSerializerNamespaces(); ns.Add(「」,「」); ser.Serialize(Console.Out,order,ns);' - 那麼沒有輸出'xmlns'條目 – 2013-05-08 08:33:22

3

我會嘗試使用LINQ到XML此:

using System; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main() 
     { 
      XElement root = 
       new XElement("order", 
        new XElement("clientId", 1001), 
        new XElement("config", 
         new XElement("properties", 
          new XElement("entry", new XAttribute("key", "RecordTotal"), 10), 
          new XElement("entry", new XAttribute("key", "InputFileName"), "name"), 
          new XElement("entry", new XAttribute("key", "ConfigName"), "COMMON"), 
          new XElement("entry", new XAttribute("key", "DeliveryDate"), "15-FEBRUARY-2013"), 
          new XElement("entry", new XAttribute("key", "Qualifier"), "name")), 
         new XElement("id", 19)), 
        new XElement("orderID", 58239346) 
      ); 

      Console.WriteLine(root); 
     } 
    } 
} 

通過比較的方式,如果你想多屬性元素所以XML是這樣的:

<order> 
    <clientId>1001</clientId> 
    <config> 
    <properties> 
     <property> 
     <entry key="RecordTotal">10</entry> 
     <entry key="InputFileName">name</entry> 
     <entry key="ConfigName">COMMON</entry> 
     <entry key="DeliveryDate">15-FEBRUARY-2013</entry> 
     <entry key="Qualifier">name</entry> 
     </property> 
     <property> 
     <entry key="RecordTotal">15</entry> 
     <entry key="InputFileName">othername</entry> 
     <entry key="ConfigName">UNCOMMON</entry> 
     <entry key="DeliveryDate">23-FEBRUARY-2013</entry> 
     <entry key="Qualifier">qname</entry> 
     </property> 
    </properties> 
    <id>19</id> 
    </config> 
    <orderID>58239346</orderID> 
</order> 

你的代碼看起來是這樣的:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main() 
     { 
      XElement root = 
       new XElement("order", 
        new XElement("clientId", 1001), 
        new XElement("config", 
         new XElement("properties", 
          createEntries(getEntries())), 
         new XElement("id", 19)), 
        new XElement("orderID", 58239346) 
      ); 

      Console.WriteLine(root); 
     } 

     static IEnumerable<Entry> getEntries() 
     { 
      yield return new Entry 
      { 
       RecordTotal = 10, 
       InputFileName = "name", 
       ConfigName = "COMMON", 
       DeliveryDate = "15-FEBRUARY-2013", 
       Qualifier  = "name" 
      }; 

      yield return new Entry 
      { 
       RecordTotal = 15, 
       InputFileName = "othername", 
       ConfigName = "UNCOMMON", 
       DeliveryDate = "23-FEBRUARY-2013", 
       Qualifier  = "qname" 
      }; 
     } 

     static IEnumerable<XElement> createEntries(IEnumerable<Entry> entries) 
     { 
      return from entry in entries 
        select new XElement(
         "property", 
         new XElement("entry", new XAttribute("key", "RecordTotal"), entry.RecordTotal), 
         new XElement("entry", new XAttribute("key", "InputFileName"), entry.InputFileName), 
         new XElement("entry", new XAttribute("key", "ConfigName"), entry.ConfigName), 
         new XElement("entry", new XAttribute("key", "DeliveryDate"), entry.DeliveryDate), 
         new XElement("entry", new XAttribute("key", "Qualifier"),  entry.Qualifier)); 
     } 
    } 

    sealed class Entry 
    { 
     public int RecordTotal; 
     public string InputFileName; 
     public string ConfigName; 
     public string DeliveryDate; 
     public string Qualifier; 
    } 
}