繁体   English   中英

XML XElement建立xml文件

[英]XML XElement building an xml document

嗨,我是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>

如您所见,符号是问题。

您正在将别名设置为您的根元素,应为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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM