1

有人能把我引向正确的方向吗?

我想要做的是从 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>
4

2 回答 2

1

您每次都在添加字段元素。这不是 XML 问题,而是逻辑问题。您的问题是您在 for 循环中添加了 hte“字段”元素。构建所有元素,然后将它们全部添加到单个“字段”元素中。或者,先构建 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() 将元素添加到列表中,然后将列表添加到较大的元素中。

于 2013-07-19T16:54:32.730 回答
0

你需要移动

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;

}

于 2013-07-19T16:54:54.100 回答