- well-known web pages, which are also an analogue of XML with weak checks.

XML Reader/Writer Objects, FastInfoset, HTML extend text file reading capabilities with built-in processing of markup tags.

They are also used for DOMBuilder/DOMRecord objects (see below) as data sources.

XML 1C files contain text information, that is, they are text files. The 1C XML Reader and 1C Write XML objects are an “add-on” that makes it easier to work with XML tags in a 1C file.

The objects ReadingFastInfoset 1C and WritingFastInfoset 1C, ReadingHTML 1C and WritingHTML 1C are completely similar to ReadingXML 1C and WritingXML 1C and serve to work in the same way with other formats.

XML 1C file used in the examples

Directory>

Directory>

Text valueProps>
Directory>
Configuration>

Example 1: Reading XML file 1C into the value tree using XML Reading 1C

//open the XML 1C file for reading using ReadingXML 1C
File = New ReadXML();
File.OpenFile("D:\ConfigurationStructure.xml");

//prepare the value tree
//each XML branch can have a name, attributes and value
dzXML = NewValueTree();
dzXML.Columns.Add("Name");
dzXML.Columns.Add("Value");
dzXML.Columns.Add("Attributes");

//since an XML string can have several attributes, we will write them in the values ​​table
//each attribute has a name and a value
tAttributes = New ValueTable();
tAttributes.Columns.Add("Name");
tAttributes.Columns.Add("Value");

//the nesting level will help us understand when we need to add a nested branch, and when we need to go back up a level
Nesting Level = 0;
//current line is a tree line, will change as nesting increases
CurrentRow = Undefined;
//reading the XML 1C file is not done line by line, but according to the structure; when the file ends, reading will return FALSE
While File.Read() Loop

//we are interested in three types of nodes - the beginning of the element, the text (the value of the element) and the end of the element (to return to the top level)
If File.NodeType = XMLNodeType.ElementStart Then

Nesting Level = Nesting Level + 1;

//if this is the first line, then add it to the very top of the tree and save only the name
If CurrentRow = Undefined Then
CurrentRow = dXML.Rows.Add();
CurrentLine.Name = File.Name;
Continue;
Otherwise
//nested lines
CurrentRow = CurrentRow.Rows.Add();
CurrentLine.Name = File.Name; //save the name

//does this have XML element attributes?
If File.NumberAttributes() > 0 Then
//if yes, copy the prepared empty table to save attributes
tAttributesNode = tAttributes.Copy();
//cycle through the number of attributes of this element
For Account = 0 by File.Number of Attributes()-1 Cycle
//for each attribute, remember the name and value
Row = tNodeAttributes.Add();
Line.Name = File.AttributeName(Sch);
Row.Value = File.AttributeValue(Ac);
EndCycle;
//save the element's attribute table to the current line
CurrentRow.Attributes = tNodeAttributes;
endIf;
endIf;

ElseIf File.NodeType = XMLNodeType.EndElement Then
//at the beginning of the element we increase the nesting level, at the end of the element we decrease it
Nesting Level = Nesting Level - 1;
//return the current line one level up
CurrentRow = CurrentRow.Parent;

ElseIf File.NodeType = XMLNodeType.Text Then
//if the element has a value, just save it
CurrentRow.Value = File.Value;

endIf;

EndCycle;

File.Close();

Example 2. Recording a 1C XML file using the 1C Record XML object

//create a file Record XML 1C
File = NewXMLRecord();
File.OpenFile("D:\ConfigurationStructure.xml", "UTF-8");
File.WriteElementStart("Configuration");

//use metadata to traverse all directories (for more details, see "Working with metadata")
For each Directory from Metadata.Directories Cycle

//WriteStartofElement - opens a new [subordinate] branch
File.WriteElementStart("Directory");
//WriteAttribute - writes an attribute to a previously opened branch
File.WriteAttribute("Name", Directory.Name);
File.WriteAttribute("Synonym", Directory.Synonym);

//using metadata we go through all the directory details
For each Props from the Directory.Props Cycle




EndCycle;

//use metadata to traverse all tabular parts of the directory
For each PM from the Directory. Tabular Parts of the Cycle
File.WriteElementStart("TabularPart");
File.WriteAttribute("Name", PM.Name);
File.WriteAttribute("Synonym", PM.Synonym);

For each Props from PM.Props Cycle
File.WriteElementStart("Props");
File.WriteAttribute("Name", Attributes.Name);
File.WriteAttribute("Synonym", Attributes.Synonym);
File.WriteEndElement();
EndCycle;

File.WriteEndElement();
EndCycle;

//WriteEndElement - “closes” the branch previously opened using WriteBeginElement
File.WriteEndElement();
EndCycle;

File.WriteEndElement();
File.Close();

In this article I want to describe a method for writing and reading XML files using the XmlSerializer class. This class is in the System.Xml.Serialization namespace of the System.Xml.dll assembly. It appeared quite a long time ago - back in the first versions of .NET. The object of this class has two interesting methods - Serialize and Deserialize. This article is about how to read or write an XML file of almost any structure, describing it with simple classes in C#. To understand that using these methods is very convenient, I will give an example of a class:

Public class Item ( public int id; public string name; public List values; public bool CurrentStatus; ) This class can be easily converted into the following XML:12.34 Element_Name> 56.78 Element_Name> Collection_Name> Root_Element_Name> To do this, it is enough to implement two methods that encapsulate the body of serialization (SaveXml) and deserialization (LoadXml) methods: public static bool SaveXml(object obj, string filename) ( bool result = false; using ( StreamWriter writer = new StreamWriter(filename)) ( try ( XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer serializer = new XmlSerializer(obj.GetType()); serializer.Serialize(writer, obj, ns); result = true; ) catch (Exception e) ( // Logging ) finally ( writer.Close(); ) ) return result; ) public static object LoadXml(Type type, string filename) ( object result = null ; using (StreamReader reader = new StreamReader(filename)) ( try ( XmlSerializer serializer = new XmlSerializer(type); result = serializer.Deserialize(reader); ) catch (Exception e) ( // Logging ) finally ( reader.Close( ); ) ) return result; ) Now I will give a complete example of using the above methods: Item item = new Item(); item.id = 42; item.name = "Item name"; item.values ​​= new List(); item.values.Add(12.34); item.values.Add(56.78); SaveXml(item, "d:\\obj.xml"); Item obj = LoadXml(typeof(Item), "d:\\obj.xml") as Item; Now a little about the flexibility of this XML serialization method. The first question is how to serialize a class member as a node attribute. To do this, just attach the attribute to it. You can rename this attribute using the parameter: . If we need to save a class member as a regular node, then we don’t have to write anything. Although, if we need to rename the node itself, we can insert the . Regarding collections of objects, it should be noted that renaming collection nodes is as simple as ordinary elements. To do this, the attributes and are inserted before the collection. As a parameter for these attributes, you can pass the names of the nodes in which the collection itself and its elements will be stored, respectively. If we don't want to save an element in XML, we can mark it with the . The described method allows you to very quickly “teach” the program how to work with various XML. And this is not necessary for everyone XML format write separate code. It is enough to declaratively describe the format in the form of a C# class with the necessary attributes.