将子元素添加到XML中

本文关键字:XML 添加 元素 | 更新日期: 2023-09-27 18:23:53

有人能帮我找到正确的方向吗?

我想做的是从WebMethod获得输出:

我从数据库中获取值;

1694.152344;1694.092285;1693.972168;1693.852051
2013-07-10 20:00:00;2013-07-10 19:00:00;2013-07-10 18:00:00;2013-07-10 17:00:00

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<FieldsRoot xmlns="http://appserver.weevio.se/schema/SDKr1/Fields.xsd">
<Fields ok="true" nodeId="TEST_1.HW">
    <Numeric value="1694.152344" datum="2013-07-10 20:00:00" status="16" />
    <Numeric value="1694.092285" datum="2013-07-10 19:00:00" status="16" />
    <Numeric value="1693.972168" datum="2013-07-10 18:00:00" status="16" />
    <Numeric value="1693.852051" datum="2013-07-10 17:00:00" status="16" />
</Fields>
</FieldsRoot>

我的代码看起来像:

[WebMethod]
public XElement Sum2()
{
    XNamespace defaultNamespace = XNamespace.Get("http://appserver.weevio.se/schema/SDKr1/Fields.xsd");
    XElement test = new XElement(new XElement(defaultNamespace + "FieldsRoot"));
    string value = "1694.152344;1694.092285;1693.972168;1693.852051";
    string datum = "2013-07-10 20:00:00;2013-07-10 19:00:00;2013-07-10 18:00:00;2013-07-10 17:00:00";
    string[] valueA = value.Split(';');
    string[] datumA = datum.Split(';');
    int d = 0;
    foreach (var customer in valueA)
    {
        XElement xElement = new XElement(defaultNamespace + "Numeric",
                new XAttribute("value", valueA[d]),
                new XAttribute("datum", datumA[d]),
                new XAttribute("status", 16)
        );
        test.Add(new XElement(defaultNamespace + "Fields", new XAttribute("ok", "true"), new XAttribute("nodeId", "TEST.HW"), xElement));
        d = d + 1;
    }
    return test;
}

我得到的是:

<?xml version="1.0" encoding="utf-8"?>
<FieldsRoot xmlns="http://appserver.weevio.se/schema/SDKr1/Fields.xsd">
  <Fields ok="true" nodeId="TEST.HW">
    <Numeric value="1694.152344" datum="2013-07-10 20:00:00" status="16" />
  </Fields>
  <Fields ok="true" nodeId="TEST.HW">
    <Numeric value="1694.092285" datum="2013-07-10 19:00:00" status="16" />
  </Fields>
  <Fields ok="true" nodeId="TEST.HW">
    <Numeric value="1693.972168" datum="2013-07-10 18:00:00" status="16" />
  </Fields>
  <Fields ok="true" nodeId="TEST.HW">
    <Numeric value="1693.852051" datum="2013-07-10 17:00:00" status="16" />
  </Fields>
</FieldsRoot>

将子元素添加到XML中

您每次都要添加fields元素。这不是一个XML问题,而是一个逻辑问题。您的问题是在for循环中添加了"Fields"元素。构建所有元素,然后将它们全部添加到单个"Fields"元素中。或者,先构建fields元素,然后向其中添加子元素。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Sum2().ToString());
            Console.WriteLine();
            Console.WriteLine(Sum2Working().ToString());
            Console.Read();
        }
        public static XElement Sum2()
        {
            XNamespace defaultNamespace = XNamespace.Get("http://appserver.weevio.se/schema/SDKr1/Fields.xsd");
            XElement test = new XElement(new XElement(defaultNamespace + "FieldsRoot"));
            string value = "1694.152344;1694.092285;1693.972168;1693.852051";
            string datum = "2013-07-10 20:00:00;2013-07-10 19:00:00;2013-07-10 18:00:00;2013-07-10 17:00:00";
            string[] valueA = value.Split(';');
            string[] datumA = datum.Split(';');
            int d = 0;
            foreach (var customer in valueA)
            {
                XElement xElement = new XElement(defaultNamespace + "Numeric",
                        new XAttribute("value", valueA[d]),
                        new XAttribute("datum", datumA[d]),
                        new XAttribute("status", 16)
                );
                test.Add(new XElement(defaultNamespace + "Fields", new XAttribute("ok", "true"), new XAttribute("nodeId", "TEST.HW"), xElement));
                d = d + 1;
            }
            return test;
        }
        public static XElement Sum2Working()
        {
            XNamespace defaultNamespace = XNamespace.Get("http://appserver.weevio.se/schema/SDKr1/Fields.xsd");
            XElement test = new XElement(new XElement(defaultNamespace + "FieldsRoot"));
            string value = "1694.152344;1694.092285;1693.972168;1693.852051";
            string datum = "2013-07-10 20:00:00;2013-07-10 19:00:00;2013-07-10 18:00:00;2013-07-10 17:00:00";
            string[] valueA = value.Split(';');
            string[] datumA = datum.Split(';');
            int d = 0;
            var fields = new List<XElement>();
            foreach (var customer in valueA)
            {
                XElement xElement = new XElement(defaultNamespace + "Numeric",
                        new XAttribute("value", valueA[d]),
                        new XAttribute("datum", datumA[d]),
                        new XAttribute("status", 16)
                );
                fields.Add(xElement);
                d = d + 1;
            }
            test.Add(new XElement(defaultNamespace + "Fields", new XAttribute("ok", "true"), new XAttribute("nodeId", "TEST.HW"), fields));
            return test;
        }
    }
}

如您所见,Sum2Working()将元素添加到列表中,然后将列表添加到较大的元素中。

您需要移动

test.Add(new XElement(defaultNamespace + "Fields", new XAttribute("ok", "true"), new XAttribute("nodeId", "TEST.HW"), xElement));

在foreach循环之外,以实现这一点。

尝试

[WebMethod]
public XElement Sum2()
{
XNamespace defaultNamespace = XNamespace.Get("http://appserver.weevio.se/schema/SDKr1/Fields.xsd");
XElement test = new XElement(new XElement(defaultNamespace + "FieldsRoot"));
string value = "1694.152344;1694.092285;1693.972168;1693.852051";
string datum = "2013-07-10 20:00:00;2013-07-10 19:00:00;2013-07-10 18:00:00;2013-07-10 17:00:00";
string[] valueA = value.Split(';');
string[] datumA = datum.Split(';');
int d = 0;
Xelement numericElement = new Xelement(); 
foreach (var customer in valueA)
{
    XElement xElement = new XElement(defaultNamespace + "Numeric",
            new XAttribute("value", valueA[d]),
            new XAttribute("datum", datumA[d]),
            new XAttribute("status", 16)
    );
    numericElement.Add(xElement);
    d = d + 1;
}
test.Add(new XElement(defaultNamespace + "Fields", new XAttribute("ok", "true"), new XAttribute("nodeId", "TEST.HW"), numericElement));
return test;

}