Perso...">

XML processing with F # TypeProvider

Given the following XML structure:

<?xml version="1.0" encoding="utf-8"?>
<Persons>
  <Person>
    <Name>Person 1</Name>
    <Age>30</Age>
  </Person>
  <Person>
    <Name>Person 2</Name>
    <Age>32</Age>
  </Person>
</Persons>

I want to add Person to the collection, and I was hoping this could be done using the Xml TypeProvider API.

My approach is as follows:

type PersonXmlProvider = XmlProvider<""".\Persons.xml""">

let personsXml = PersonXmlProvider.GetSample()
personsXml.XElement.Add(new PersonXmlProvider.Person("Person 3", 33))
personsXml.XElement.Save("Persons.xml")

What happens: A person is added to the xml collection and written to a file.

Unexpected: But the encoding is incorrect, because the XML tags are encoded as &lt;they were &gt;instead of <and>.

What am I missing?

The documentation states that UTF-8 is the default.

Update

XML result

<?xml version="1.0" encoding="utf-8"?>
<Persons>
  <Person>
    <Name>Person 1</Name>
    <Age>30</Age>
  </Person>
  <Person>
    <Name>Person 2</Name>
    <Age>32</Age>
  </Person>&lt;Person&gt;
  &lt;Name&gt;Person 3&lt;/Name&gt;
  &lt;Age&gt;33&lt;/Age&gt;
&lt;/Person&gt;
</Persons>
+4
source share
1 answer

The method XElement.Add(content: obj)accepts a content object.

PersonXmlProvider.Person.ToString XML, node .

, , node , XML- .

P.S.

, XElement .

let person = new PersonXmlProvider.Person("Person 3", 33)
personsXml.XElement.Add(person.XElement)
+5

Source: https://habr.com/ru/post/1666527/


All Articles