Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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# 使用C从XML创建XML_C#_Xml_Xelement - Fatal编程技术网

C# 使用C从XML创建XML

C# 使用C从XML创建XML,c#,xml,xelement,C#,Xml,Xelement,我有一个XML文件。我正在寻找帮助,以便从这个xml文件创建多个xml文件。新的xml将具有相同EmpID的所有节点- <?xml version="1.0" encoding="utf-8"?> <Connected> <Emp> <A.EMPLID>1</A.EMPLID> <A.Phone>12##</A.Phone> </E

我有一个XML文件。我正在寻找帮助,以便从这个xml文件创建多个xml文件。新的xml将具有相同EmpID的所有节点-

<?xml version="1.0" encoding="utf-8"?>
      <Connected>
      <Emp>
          <A.EMPLID>1</A.EMPLID>
          <A.Phone>12##</A.Phone>
      </Emp>
      <Emp>
          <A.EMPLID>1</A.EMPLID>
          <A.Add>XXXXXXX</A.Add>
      </Emp>
      <Emp>
          <A.EMPLID>2</A.EMPLID>
         <A.Phone>##34</A.Phone>
      </Emp>
      <Emp>
         <A.EMPLID>3</A.EMPLID>
      </Emp>
      <Emp>
         <A.EMPLID>3</A.EMPLID>
         <A.Add>XXXXXXX</A.Add>
     </Emp>
    </Connected>
但它创造了这样的东西:

<?xml version="1.0" encoding="utf-8"?>
<Connected>
<Emp>


<A.EMPLID>1</A.EMPLID>


<A.Phone>12##</A.Phone>


<A.Add>XXXXXXX</A.Add>


</Emp>


</Connected>   
我需要为Empld生成3个XML,但节点的顺序应该完全相同

使用xml linq:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string ident = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Connected></Connected>";

            XDocument doc = XDocument.Load(FILENAME);

            var groups = doc.Descendants("Emp").GroupBy(x => (string)x.Element("A.EMPLID")).ToList();
            foreach (var group in groups)
            {
                XDocument doc1 = XDocument.Parse(ident);
                XElement root = doc1.Root;

                root.Add(group);

                doc1.Save(@"c:\temp\test" + group.Key + ".xml");
            }

        }
    }
}

实现这一点的另一种方法是使用XPath

var empElements = xmlDocument.SelectNodes("//Emp[A.EMPLID=1]");
在本例中,上述查询将返回属于特定ID的所有节点。

请回答您的问题并分享当前生成错误XML的完整代码,即a?这与您之前的问题和建议有什么关系?这是同一个问题,还是另一个问题?
      XElement x = new XElement("Connected",new XElement("Emp",new XElement("A.EMPLID", group.Key),group.Select(g => g.Elements().Where(e =>e.Name != "A.EMPLID"))));
<?xml version="1.0" encoding="utf-8"?>
<Connected>
<Emp>


<A.EMPLID>1</A.EMPLID>


<A.Phone>12##</A.Phone>


<A.Add>XXXXXXX</A.Add>


</Emp>


</Connected>   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string ident = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Connected></Connected>";

            XDocument doc = XDocument.Load(FILENAME);

            var groups = doc.Descendants("Emp").GroupBy(x => (string)x.Element("A.EMPLID")).ToList();
            foreach (var group in groups)
            {
                XDocument doc1 = XDocument.Parse(ident);
                XElement root = doc1.Root;

                root.Add(group);

                doc1.Save(@"c:\temp\test" + group.Key + ".xml");
            }

        }
    }
}
var empElements = xmlDocument.SelectNodes("//Emp[A.EMPLID=1]");