There is an xml document of the following type:

<nsiOrganization> <oos:regNumber></oos:regNumber> <oos:shortName></oos:shortName> <oos:fullName></oos:fullName> <oos:factualAddress> <oos:OKATO></oos:OKATO> <oos:addressLine></oos:addressLine> <oos:building></oos:building> <oos:country> <oos:countryCode></oos:countryCode> <oos:countryFullName></oos:countryFullName> </oos:country> ... ... ... ... ... </nsiOrganization> <nsiOrganization> ... ... ... ... ... </nsiOrganization> ... ... ... ... ... 

how to split each <nsiOrganization> by different xml documents?

    5 answers 5

     // Загружаем xml файл XDocument xDoc = XDocument.Load("C:/myXml.xml"); // делим на части XElement[] xmls = xDoc.Root.Elements().ToArray(); for (int i = 0; i < xmls.Length; i++) { // Записываем каждый элемент в разные файлы using (var file = File.CreateText(string.Format("xml{0}.xml", i + 1))) { //xmls[i] - содержит <nsiOrganization> file.Write(xmls[i].ToString()); } 
    • in xmls[i] not clear what. not separate <nsiOrganization> but everything and how horrible .... ( - Side Kick

    It turned out to do something like that ...

     for (int j = 0; j < xmlFilesInFolder.Count; j++) { export exp = new export(); string usfile = @"C:\Temp\xml\Organization\" + xmlFilesInFolder[j]; using (FileStream stream = new FileStream(usfile, FileMode.Open)) { XmlSerializer xs = new XmlSerializer(typeof(export)); exp = (export)xs.Deserialize(stream); for (int b = 0; b < ((exportNsiOrganizationList)exp.Items[0]).nsiOrganization.Length; b++) { Object myOrgan = ((exportNsiOrganizationList)exp.Items[0]).nsiOrganization[b]; XmlSerializer xss = new XmlSerializer(typeof(zfcs_nsiOrganizationType)); Stream fs = new FileStream(@"C:\Temp\xml\parsxml\_" + filesInFtp[i] + "org_" + (b + 1) + ".xml", FileMode.Create); XmlWriter writer = new XmlTextWriter(fs, Encoding.UTF8); xss.Serialize(writer, myOrgan); writer.Close(); } } } 

      You can try using regular expressions. The truth is that there is more likely by trial and error, but the algorithm is about the following 1) read xml into line 2) compose an expression like

       "<nsiOrganization>(.|\W)+</nsiOrganization>" 

      3) you will get an array of type match, which you can write to different xml. Again, I repeat that there are a lot of nuances, for example, the expression can take the first and the last one, does it fit the idea? ) So it will need to take into account.

      PS I do not know whether it will work, but you can try

       "<nsiOrganization>[^(<nsiOrganization>)]+</nsiOrganization>" 

        You can do all the processing in the streams, respectively, directing the output streams to the file.

         using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; namespace ConsoleApplication6 { class Program { static List<String> XmlList = new List<String>(); static void Main(string[] args) { String xml = @"<array> <nsiOrganization>value1</nsiOrganization> <nsiOrganization>value2</nsiOrganization> </array>"; using (StringReader stringReader = new StringReader(xml)) { processXml(stringReader); } foreach(String s in XmlList) { Console.WriteLine(s); Console.WriteLine(); } } static void processXml(TextReader textReader) { using (XmlReader xmlReader = XmlReader.Create(textReader)) { xmlReader.MoveToContent(); xmlReader.ReadStartElement("array"); while (xmlReader.EOF == false) { if (xmlReader.NodeType == XmlNodeType.Element && // xmlReader.NamespaceURI == "" && xmlReader.LocalName == "nsiOrganization") { using (XmlReader elementReader = xmlReader.ReadSubtree()) { WriteElement(elementReader); } } else { xmlReader.Skip(); } } } } static void WriteElement(XmlReader xmlReader) { xmlReader.MoveToContent(); StringBuilder stringBuilder = new StringBuilder(); using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder)) { xmlWriter.WriteNode(xmlReader, true); } XmlList.Add(stringBuilder.ToString()); } } } 
           XDocument doc = XDocument.Load("file.xml"); var i = 0; foreach(XElement el in doc.Elements(XName.Get("nsiOrganization"))) { var file = File.CreateText($"путь{i}.xml"); file.Write(el.ToString()); i++; } 
          • Well, almost, I would only save not in a bare XEelement, but in an XDocument with copying declarations for independent use. judging by the prefixes of the elements, there also should be a scheme in principle, a link to which is also not a sin to take from the original document - rdorn