【问题标题】:Build XML Dynamically using c#使用 c# 动态构建 XML
【发布时间】:2011-05-11 02:43:22
【问题描述】:

我必须根据用户输入动态创建一个 XML 文件。

这是我想出的,我遇到了两个问题。

  1. 如果有相同元素的集合 (MaxOccurs = 10) (例如,如果用户输入了 4 个帐户,那么我的代码应该如何)
  2. 如果有选择选项。根据选择的元素,子元素应该改变。

请有人帮助我。

提前致谢

BB

我的代码:

XElement req = 
    new XElement("order",
        new XElement("client", 
            new XAttribute("id", clientId),
            new XElement("quoteback", 
                new XAttribute ("name",quotebackname)
                )  
            ),
        new XElement("accounting",
            new XElement("account"),
            new XElement("special_billing_id")
            ),
        new XElement("products",
            new XElement(
                **productChoiceType**,
                ***** HERE THE ELEMENTS WILL CHAGE BASED ON  **productChoiceType**           
                )
            )
        )
    );

【问题讨论】:

    标签: c# xml xelement


    【解决方案1】:

    LINQ 在这样的事情上派上用场:

    XElement req = 
        new XElement("order",
            new XElement("client", 
                new XAttribute("id",clientId),
                new XElement("quoteback", new XAttribute ("name",quotebackname))  
                ),
            new XElement("accounting",
                new XElement("account"),
                new XElement("special_billing_id")
                ),
                new XElement("products", 
                    new XElement(productChoices.Single(pc => pc.ChoiceType == choiceType).Name, 
                        from p in products
                        where p.ChoiceType == choiceType
                        select new XElement(p.Name)
                  )
              )
          );
    

    【讨论】:

    • StriplingWarrior 非常感谢。根据选择的(p.Name),我必须向“产品”添加一组完全不同的元素。
    • 说如果 p.Name 是 CLUEAuto 那么我必须将参数、pnc、用法元素添加到“产品”。如果 p.Name 是 Mortgae,那么我必须在 Products 中添加参数、RiskAddress、CurrentAddress、PreviousAddress、Mortgage 元素
    • new XElement("parameter),new XElement("pnc"),new XElement("usage", ClueAutoUsageEnum),
    • @BumbleBee:应用你看到我使用的相同原则:...new XElement(p.Name, from t in thingRepository.GetByProductName(p.Name) select new Element(t.Whatever)
    【解决方案2】:

    改用XmlWriter 对象,至少在imo 中更容易做你想做的事情。然后你可以像这样构造它:

    XmlWriter w = XmlWriter.Create(outputStream);
    w.WriteStartElement("order");
    
    w.WriteStartElement("client");
    w.WriteAttributeString("id", clientId);
    
    // ...
    w.WriteElementString("product", "1");
    w.WriteElementString("product", "2");
    w.WriteElementString("product", "3");
    w.WriteElementString("product", "4");
    
    // etc....
    
    w.WriteEndElement(); // client
    
    w.WriterEndElement(); // order
    

    【讨论】:

      【解决方案3】:

      或者为您想要转换为 XML 的每种类型创建一个类并使用 XmlSerializer。

      <XmlElement("order")> _
      Public Class Order
          <XmlElement("accounting")> _
          Dim accounts As List(Of Account)
          ...
      End Class
      
      Dim xmlSer as New XmlSerialzer(GetType(Accounting))
      xmlSer.Serialize(myXmlWriter, myObjInstance)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-29
        • 1970-01-01
        • 2014-12-15
        相关资源
        最近更新 更多