使用 C# 动态构建 XML

发布于 2024-10-03 00:47:10 字数 873 浏览 1 评论 0原文

我必须根据用户输入动态创建一个 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**           
                )
            )
        )
    );

I have to create an XML file dynamically based on the user input.

Here is what I came up with and I am struck up with two issues.

  1. if there is a collection of same element (MaxOccurs = 10)
    (For example if the user entered 4 accounts then how should my code be)
  2. If there is a choice option. Based on the element chosen the child elements should change.

Somebody please help me.

Thanks in advance

BB

My code :

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**           
                )
            )
        )
    );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诠释孤独 2024-10-10 00:47:10

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

LINQ comes in handy for things like this:

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)
              )
          )
      );
秋叶绚丽 2024-10-10 00:47:10

使用 XmlWriter 对象代替,至少在我看来是这样更容易做你想做的事情。然后你可以将其构造如下:

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

Use an XmlWriter object instead, at least imo it is easier to do the sort of things you want. You can then structure it something like:

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
歌枕肩 2024-10-10 00:47:10

或者为要转换为 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)

Or create a class for each type that you want to convert to XML and use the 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文