使用LINQ创建KML到XML [英] Creating KML with Linq to XML

查看:123
本文介绍了使用LINQ创建KML到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

9个月前,我创建了一套符合KML 2.2元素的C#类。所以,你可以做这样的事情myPlacemark =新地标(名称);在内部,它使用的XmlDocument,XmlElement的,等创造了各种节点和标签。这是一个内存猪,它可能会更快。不,我没有使用XSD生成的类。



我看到有使用LINQ上阅读文章和解析KML。但是,有没有人使用LINQ到XML创建KML?如果没有,你认为什么是创建一个纲领性的最佳方法。易于使用的一系列类抽象KML?将性能使用LINQ to XML改进?


解决方案

不KML,但这里使用的System.Xml.Serialization <示例/ p>

由于这两个类(使用属性)和初始化:

  [XmlRoot(书目)] 
公共类书目
{
[的XmlElement(BookData)]
公开名单<图书>图书=新的List<图书>();
}

公共类图书
{
[XmlElement的(标题)]
公共字符串名称{搞定;组; }
[XmlAttribute(国际标准书号)]
公共字符串ISBN {搞定;组; }
}

变种书目=新书目
{
书籍= {新的图书{标题=一生只有一次,ISBN =135468}}
};

您可以序列化成XML,像这样:

  VAR串=新的XmlSerializer(typeof运算(书目)); 
使用(VAR作家=新的StreamWriter(YourFileNameHere))
{
serializer.Serialize(作家,书目);
}

这是等同的LINQ到XML将看起来像这样(未测试)

 的XElement bookXML = 
新的XElement(书目,从书中
在bookList.Books
选择新的XElement(BookData,
新的XElement(标题,book.Title),
新XAttribute(国际标准书号,book.ISBN)

);



结论,无论是比使用XmlDocument的清洁剂,XmlSerializer的时间较短,LINQ到XML为您提供了更大的灵活性( XmlSerializer的是在你的类结构是如何的不同可以将XML结构)方面相当刚性。


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?

解决方案

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).

这篇关于使用LINQ创建KML到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆