如何从ssms表创建带有嵌套xml的webapi?

时间:2018-11-08 09:27:01

标签: c# xml asp.net-web-api nested

我用C#和ssms表制作了一个Web api。结果是仅包含元素的xml。

//This is only an example


 <?xml version="1.0" encoding="UTF-8"?>
    <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>

如何格式化这样的xml嵌套格式,并从ssms表元素添加可选的不同标头??

<?xml version="1.0" encoding="UTF-8"?>
    <note>
      <to>Tove</to>
       <example>
        <from>Jani
         <heading>Reminder</heading>
        </from>
        <body>Don't forget me this weekend!</body>
      </example>
    </note>

1 个答案:

答案 0 :(得分:0)

使用xml linq:

SUBSYSTEM="usb", KERNELS="1-1.3.4.4", SYMLINK+="hub0"
SUBSYSTEM="usb", KERNELS="1-1.3.4.3", SYMLINK+="hub1"
SUBSYSTEM="usb", KERNELS="1-1.3.4.2", SYMLINK+="hub2"

如果您是从头开始创建的

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

namespace ConsoleApplication78
{
    class Program
    {

        static void Main(string[] args)
        {

            string xml = 
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<note>" +
                    "<to>Tove</to>" +
                    "<from>Jani</from>" +
                    "<heading>Reminder</heading>" +
                    "<body>Don't forget me this weekend!</body>" +
                "</note>";

            XDocument doc = XDocument.Parse(xml);

            XElement from = doc.Descendants("from").FirstOrDefault();
            XElement heading = doc.Descendants("heading").FirstOrDefault();
            XElement body = doc.Descendants("body").FirstOrDefault();

            from.Add(heading);

            XElement example = new XElement("example", new object[] {from,body});
            heading.Remove();
            body.Remove();
            from.ReplaceWith(example);


        }
    }
}