Creating KML with Linq for XML

About 9 months ago, I created a set of classes in C # that correspond to KML 2.2 elements. That way you can do things like myPlacemark = new label ("name"); Inside, it uses XmlDocument, XmlElement, etc. To create various nodes and tags. It is a pig with memory, and it can probably be faster. No, I did not create classes using XSD.

I see that there are records for reading and analyzing KML using Linq. However, did anyone use Linq for XML to create KML? If not, then what do you think is the best approach to creating a pro-game. easy-to-use set of classes for abstract KML? Does performance improve with Linq for XML?

+3
source share
2 answers

Not KML, but here is an example using System.Xml.Serialization

Given these two classes (with attributes) and initialization:

[XmlRoot("BookList")]
public class BookList
{
    [XmlElement("BookData")]
    public List<Book> Books = new List<Book>();
}

public class Book
{
    [XmlElement("Title")]
    public string Title { get; set; }
    [XmlAttribute("isbn")]
    public string ISBN { get; set; }
}

var bookList = new BookList
{
    Books = { new Book { Title = "Once in a lifetime", ISBN = "135468" } }
};

You can serialize in xml as follows:

var serializer = new XmlSerializer(typeof(BookList));
using (var writer = new StreamWriter("YourFileNameHere")) 
{
    serializer.Serialize(writer, bookList); 
}

Equivalent Linq to Xml will look something like this (not verified)

XElement bookXML = 
    new XElement("BookList", 
        from book in bookList.Books
        select new XElement("BookData",
            new XElement("Title", book.Title),
            new XAttribute("isbn", book.ISBN)
        )
    );

Conclusion, both cleaner than using XmlDocument, XmlSerializer is shorter, Linq to XML gives you more flexibility (XmlSerializer is pretty "tough" in terms of how different your class structure is for your xml structure).

0
source

I did not work with KML, but the Linq to XML libraries are essentially a replacement for the XmlDocument, XmlElement, etc. in System.Xml. In terms of memory, I don't know how much better they are than your current solution.

, , , Xml, XmlSerializer Xml. , , , .

. , Xml:

<Packages>
  <Package Name="A">

  </Package>
  <Package Name="B">
      <Dependencies>
          <Dependency Package="C" />
          <Dependency Package="A" />
      </Dependencies>
  </Package>
  <Package Name="C">
      <Dependencies>
          <Dependency Package="A" />
      </Dependencies>
  </Package>
</Packages >

:

class Package 
{
    private List<Dependency> dependencies = new List<Dependency>();
    public string Name { get; set; }
    public List<Dependency> Dependencies { get { return dependencies; } set { dependencies = value; } }
}

class Dependency
{
    public string Package { get; set; }
}

System.Xml.Serialization.XmlSerializer, List Xml .

, KML, KML, , .

-1

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


All Articles