Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#XML程序附加newXElement的内部块_C#_Xml_Linq_Nodes - Fatal编程技术网

C#XML程序附加newXElement的内部块

C#XML程序附加newXElement的内部块,c#,xml,linq,nodes,C#,Xml,Linq,Nodes,我有一个C#程序,它将一个特定的XML元素内部块附加到现有的XML文件中(XML文档有457行),数据来自硬编码的xls电子表格。我将LINQ to XML与XML.subjects()和AddBeforeSelf()方法一起使用。只有当我有两条记录要追加时,程序才能正常工作,并且当我上载3条或两条以上的记录时,新的XML元素没有插入到正确的部分,第3条和后续的记录追加到内部元素的错误块上 我的程序的目标是从包含多条记录的xls电子表格中获取记录,并将它们插入到特定的内部XML节点。例如,Ord

我有一个C#程序,它将一个特定的XML元素内部块附加到现有的XML文件中(XML文档有457行),数据来自硬编码的xls电子表格。我将LINQ to XML与XML.subjects()和AddBeforeSelf()方法一起使用。只有当我有两条记录要追加时,程序才能正常工作,并且当我上载3条或两条以上的记录时,新的XML元素没有插入到正确的部分,第3条和后续的记录追加到内部元素的错误块上

我的程序的目标是从包含多条记录的xls电子表格中获取记录,并将它们插入到特定的内部XML节点。例如,OrderLine的xml元素当前位于第110行,程序应该在第110行之前插入新块,当我只有两条记录时它可以工作,但当我有两条以上的记录时它就不工作了

请参阅我的XML网格视图,注意在ReleaseLine和ShipUnit下有2条记录。后续记录必须插入到右侧节点,或者必须嵌套在ReleaseLine和ShipUnit下。谢谢你的帮助,谢谢

以下是我的代码示例:

public class EditOrder
XElement xml = XElement.Load(@"C:\OrderTest.xml");            

            var childrens = xml.Descendants().ToArray();
            var next_releaseline = childrens[110];
            var next_shipunit = childrens[110];

            Console.WriteLine(childrens[0]); //prints out the whole content

 next_orderline.AddBeforeSelf(new XElement("ReleaseLine",
        new XElement("ReleaseLineGid",
            new XElement("PackagedItemRef",
                new XElement("ItemQuantity",)

            newXelement(Test),

                ....)));

 next_shipunit.AddBeforeSelf(new XElement("ShipUnit",
        new XElement("ShipUnit",
            new XElement("WeightVolume",
                new XElement("ShipUnitContent",))));

xml.Save(@"C:\OrderTest.xml");
我从另一个类调用EditOrder类来执行程序

//Call from another class to execute the XML insertion
ProjectXML2.EditOrder.Main();
试试这个

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

namespace ConsoleApplication61
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                "<Transmission>" +
                  "<TransmissionBody>" +
                    "<Release>" +
                      "<ShiptoLocation></ShiptoLocation>" +
                      "<TimeFrame></TimeFrame>" +
                    "</Release>" +
                  "</TransmissionBody>" +
                "</Transmission>";

            XDocument doc = XDocument.Parse(xml);
            XElement releaseLine = new XElement("ReleaseLine",
                new XElement("ReleaseLineGid",
                    new XElement("PackagedItemRef",
                        new XElement("ItemQuantity"))));

            XElement shipUnit =  new XElement("ShipUnit",
                new XElement("ShipUnit",
                    new XElement("WeightVolume",
                        new XElement("ShipUnitContent"))));

            XElement release = doc.Descendants("Release").FirstOrDefault();
            release.Add(releaseLine);
            release.Add(shipUnit);
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序61
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
XDocument doc=XDocument.Parse(xml);
XElement releaseLine=新XElement(“releaseLine”,
新XElement(“ReleaseLineGid”,
新XElement(“PackagedItemRef”,
新元素(“项目数量”);
XElement shipUnit=新XElement(“shipUnit”,
新XElement(“ShipUnit”,
新的XElement(“权重卷”,
新XElement(“ShipUnitContent”);
XElement release=doc.substands(“release”).FirstOrDefault();
release.Add(releaseLine);
发布。添加(shipUnit);
}
}
}
试试这个

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

namespace ConsoleApplication61
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                "<Transmission>" +
                  "<TransmissionBody>" +
                    "<Release>" +
                      "<ShiptoLocation></ShiptoLocation>" +
                      "<TimeFrame></TimeFrame>" +
                    "</Release>" +
                  "</TransmissionBody>" +
                "</Transmission>";

            XDocument doc = XDocument.Parse(xml);
            XElement releaseLine = new XElement("ReleaseLine",
                new XElement("ReleaseLineGid",
                    new XElement("PackagedItemRef",
                        new XElement("ItemQuantity"))));

            XElement shipUnit =  new XElement("ShipUnit",
                new XElement("ShipUnit",
                    new XElement("WeightVolume",
                        new XElement("ShipUnitContent"))));

            XElement release = doc.Descendants("Release").FirstOrDefault();
            release.Add(releaseLine);
            release.Add(shipUnit);
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序61
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
XDocument doc=XDocument.Parse(xml);
XElement releaseLine=新XElement(“releaseLine”,
新XElement(“ReleaseLineGid”,
新XElement(“PackagedItemRef”,
新元素(“项目数量”);
XElement shipUnit=新XElement(“shipUnit”,
新XElement(“ShipUnit”,
新的XElement(“权重卷”,
新XElement(“ShipUnitContent”);
XElement release=doc.substands(“release”).FirstOrDefault();
release.Add(releaseLine);
发布。添加(shipUnit);
}
}
}

您确实不应该使用
var
,除非它在其他地方自我记录,例如
ToolStripMenuItem ToolStripMenuItem=new ToolStripMenuItem()
可以写为
var ToolStripMenuItem=new ToolStripMenuItem()
,因为您正在保存冗余项。在这里,我不知道这些变量是什么类型的,这会影响代码的可读性。除非它在其他地方自我记录,否则不应该使用
var
,例如
ToolStripMenuItem ToolStripMenuItem=new ToolStripMenuItem()
可以写成
var ToolStripMenuItem=new ToolStripMenuItem()
因为你在节省开支。在这里,我不知道这些变量是什么类型,这会影响代码的可读性。