Creating KML with Linq to XML

天大地大妈咪最大 提交于 2019-12-10 18:47:42

问题


About 9 months ago, I created a set of classes in C# that correspond to KML 2.2 elements. So you can do things like myPlacemark = new Placemark("name"); Internally, it uses XmlDocument, XmlElement, etc. to create the various nodes and tags. It's a memory pig and it could probably be faster. No, I didn't generate the classes using XSD.

I see there are posts on reading and parsing KML using Linq. However, has anyone used Linq to XML to create KML? If not, what do you think is the best approach to create a progammatic. easy to use set of classes to abstract KML? Would performance be improved by using Linq to XML?


回答1:


Not KML, but here's an example of using System.Xml.Serialization

Given these two classes (with the attributes) and initialisation:

[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 to xml like so:

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

An equivalent Linq to Xml would look something like this (not tested)

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 are cleaner than using XmlDocument, XmlSerializer is shorter, Linq to XML gives you greater flexibility (XmlSerializer is pretty 'rigid' in terms of how different your class structure can be to your xml structure).




回答2:


I have not worked with KML, but the Linq to XML libraries are essentially a substitute to the XmlDocument, XmlElement, etc, model in System.Xml. From a memory perspective, I do not know if they are any better than your current solution.

One approach I often use is to create a set of data classes that represent the Xml document and then use the XmlSerializer to turn the objects into Xml. I'm fairly this implementation is not a great memory hog and I think, for your purposes, this may make sense.

Here's a quick example. Say I wan to create the following 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 >

You can create the following classes:

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; }
}

Using the System.Xml.Serialization.XmlSerializer class, you can turn a List into the Xml above.

So if you create an object model for KML that easily maps to the actual KML, like the example above, it should be a somewhat straightforward implementation.



来源:https://stackoverflow.com/questions/581865/creating-kml-with-linq-to-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!