<?xml version="1.0" encoding="utf-8"?> <database> <catalog name=""> <item> <attribute name="" value="0cc56775-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="AAAAAQAAAAA=" /> <attribute name="" value="false" /> <attribute name="" value="false" /> <attribute name="" value="000000001" /> <attribute name="" value=" " /> <attribute name="" value="1111111111" /> <attribute name="" value="222222222" /> </item> <item> <attribute name="" value="0cc56776-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="AAAAAwAAAAA=" /> <attribute name="" value="false" /> <attribute name="" value="false" /> <attribute name="" value="000000002" /> <attribute name="" value="" /> <attribute name="" value="3333333333" /> <attribute name="" value="444444444" /> </item> </catalog> <catalog name=""> <item> <attribute name="" value="0cc56777-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="AAAAAgAAAAA=" /> <attribute name="" value="false" /> <attribute name="" value="false" /> <attribute name="" value="000000001" /> <attribute name="" value="" /> <attribute name="" value="15000" /> </item> <item> <attribute name="" value="0cc56778-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="AAAAAwAAAAA=" /> <attribute name="" value="false" /> <attribute name="" value="false" /> <attribute name="" value="000000002" /> <attribute name="" value="" /> <attribute name="" value="25000" /> </item> </catalog> <document name=""> <attribute name="" value="0cc56779-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="AAAAAQAAAAA=" /> <attribute name="" value="false" /> <attribute name="" value="000000001" /> <attribute name="" value="2013-08-25T21:19:56" /> <attribute name="" value="true" /> <attribute name="" value="0cc56776-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name=""> <line> <attribute name="" value="0cc56779-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="1" /> <attribute name="" value="0cc56778-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="1" /> <attribute name="" value="25000" /> <attribute name="" value="25000" /> </line> </attribute> </document> <document name=""> <attribute name="" value="0cc5677a-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="AAAAAwAAAAA=" /> <attribute name="" value="false" /> <attribute name="" value="000000002" /> <attribute name="" value="2013-08-25T21:22:49" /> <attribute name="" value="true" /> <attribute name="" value="0cc56775-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name=""> <line> <attribute name="" value="0cc5677a-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="1" /> <attribute name="" value="0cc56778-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="1" /> <attribute name="" value="25000" /> <attribute name="" value="25000" /> </line> <line> <attribute name="" value="0cc5677a-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="2" /> <attribute name="" value="0cc56777-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="2" /> <attribute name="" value="15000" /> <attribute name="" value="30000" /> </line> </attribute> </document> <document name=""> <attribute name="" value="0cc5677b-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="AAAAAgAAAAA=" /> <attribute name="" value="false" /> <attribute name="" value="000000001" /> <attribute name="" value="2013-08-25T21:23:10" /> <attribute name="" value="true" /> <attribute name="" value="0cc56776-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value=" .." /> <attribute name=""> <line> <attribute name="" value="0cc5677b-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="1" /> <attribute name="" value="0cc56777-0daa-11e3-bf95-f46d04eec7f5" /> <attribute name="" value="15000" /> <attribute name="" value="15000" /> <attribute name="" value="15000" /> </line> </attribute> </document> </database>
ExportDatabase() = XML; .(); .XML(); .("database"); ... .(); .();
Connection.ExportDatabase(path);
using System; using System.Text; using System.Xml; namespace ConsoleApplication { class Export1C { dynamic Connection; public void Connect(string path, string user = "", string password = "") { dynamic connector = Activator.CreateInstance(Type.GetTypeFromProgID("V82.COMConnector")); string connectionString = "File=\"" + path + "\""; if (user != "") connectionString += ";Usr=\"" + user + "\""; if (password != "") connectionString += ";Pwd=\"" + password + "\""; Connection = connector.Connect(connectionString); } public void Export(string path) { XmlTextWriter xml = new XmlTextWriter(path, Encoding.UTF8); xml.Formatting = Formatting.Indented; xml.WriteStartDocument(); xml.WriteStartElement("database"); // Catalogs foreach (dynamic catalog in Connection.Metadata.Catalogs) { xml.WriteStartElement("catalog"); xml.WriteAttributeString("name", catalog.Name); dynamic query = Connection.NewObject("Query"); query.Text = "select * from catalog." + catalog.Name; dynamic items = query.Execute().Unload(); for (int i = 0; i < items.Count(); i++) { xml.WriteStartElement("item"); for (int j = 0; j < items.Columns.Count(); j++) { xml.WriteStartElement("attribute"); xml.WriteAttributeString("name", items.Columns.Get(j).Name); xml.WriteAttributeString("value", Connection.XMLString(items.Get(i).Get(j))); xml.WriteEndElement(); } xml.WriteEndElement(); } xml.WriteEndElement(); } // Documents foreach (dynamic document in Connection.Metadata.Documents) { dynamic query = Connection.NewObject("Query"); query.Text = "select * from document." + document.Name; dynamic table = query.Execute().Unload(); for (int i = 0; i < table.Count(); i++) { xml.WriteStartElement("document"); xml.WriteAttributeString("name", document.Name); dynamic docref = null; for (int j = 0; j < table.Columns.Count(); j++) { xml.WriteStartElement("attribute"); string field = table.Columns.Get(j).Name; xml.WriteAttributeString("name", field); dynamic tabular = document.TabularSections.Find(field); if (tabular == null) { xml.WriteAttributeString("value", Connection.XMLString(table.Get(i).Get(j))); if (field == "") docref = table.Get(i).Get(j); } else { dynamic subquery = Connection.NewObject("Query"); subquery.Text = "select * from document." + document.Name + "." + field + " as lines where lines.Ref=&Ref"; subquery.SetParameter("Ref", docref); dynamic lines = subquery.Execute().Unload(); for (int line = 0; line < lines.Count(); line++) { xml.WriteStartElement("line"); for (int col = 0; col < lines.Columns.Count(); col++) { xml.WriteStartElement("attribute"); xml.WriteAttributeString("name", lines.Columns.Get(col).Name); string value = Connection.XMLString(lines.Get(line).Get(col)); xml.WriteAttributeString("value", value); xml.WriteEndElement(); } xml.WriteEndElement(); } } xml.WriteEndElement(); } xml.WriteEndElement(); } } xml.WriteEndElement(); xml.WriteEndDocument(); xml.Close(); } } class Program { static void Main(string[] args) { Export1C export = new Export1C(); export.Connect("D:\\TestBase", "User", "pass"); export.Export("D:\\Export.xml"); } } }
Source: https://habr.com/ru/post/191660/
All Articles