【问题标题】:XML XElement building an xml documentXML XElement 构建一个 xml 文档
【发布时间】:2015-01-12 12:11:49
【问题描述】:

您好,我是 XML 构建的新手,我以前基本没用过,我一直更喜欢 json。

我有一个解决方案,我只是用字符串制作它并转换为 XML 对象,但是我如何用 XElement 类来做呢?

这是文件:

<?xml version="1.0" encoding="utf-8"?>
 <requestblock version="3.67">
    <alias>ALIAS</alias>
    <request type="AUTH"> 
       <operation> 
          <sitereference>test12345</sitereference> 
          <accounttypedescription>TEST</accounttypedescription> 
          <parenttransactionreference>12-3-4567</parenttransactionreference> 
       </operation> 
       <merchant> 
          <orderreference>Example recurring auth</orderreference>
       </merchant> 
       <customer> </customer> 
       <billing> 
          <amount currencycode="GBP">1234</amount> 
          <subscription type="TEST">
             <number>1</number>
          </subscription> 
       </billing> 
       <settlement/>
    </request> 
 </requestblock>

我已经有部分代码是这样的:

       XElement address =
            new XElement("alias", "TEST",
            new XElement("request", new XAttribute("type", "AUTH"),
            new XElement("City", "Mercer Island"),
            new XElement("State", "WA"),
            new XElement("Postal", "68042")
       ));

但是我对别名有问题,因为别名在所有元素之后都是关闭的,而不是相同的符号:

<alias>TEST
    <request type="AUTH">
        <City>Mercer Island</City>
        <State>WA</State>
        <Postal>68042</Postal>
    </request>
</alias>

如您所见,符号是问题所在。

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    您将别名设置为根元素,它应该是 requestblock。如果你像这样从 requestblock 开始:

    XElement address =
           new XElement("requestblock", new XAttribute("version",3.67),
           new XElement("alias", "TEST"),
           new XElement("request", new XAttribute("type", "AUTH"),
           new XElement("City", "Mercer Island"),
           new XElement("State", "WA"),
           new XElement("Postal", "68042")
    

    它会给你

    <requestblock version="3.67">
       <alias>TEST</alias>
       <request type="AUTH">
          <City>Mercer Island</City>
          <State>WA</State>
          <Postal>68042</Postal>
       </request>
    </requestblock>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多