将子元素添加到XML

时间:2013-07-19 16:46:46

标签: c# xml foreach webmethod xelement

有人可以让我朝着正确的方向前进吗?

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

2 个答案:

答案 0 :(得分:1)

每次都要添加fields元素。这不是XML问题,而是逻辑问题。你的问题是你在for循环中添加了hte“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()将元素添加到列表中,然后将列表添加到更大的元素。

答案 1 :(得分: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;

}