使用 Linq to XML 创建 KML

发布于 2024-07-14 11:44:34 字数 319 浏览 4 评论 0原文

大约 9 个月前,我用 C# 创建了一组与 KML 2.2 元素相对应的类。 所以你可以做类似 myPlacemark = new Placemark("name"); 的事情 在内部,它使用 XmlDocument、XmlElement 等来创建各种节点和标签。 它是一个记忆猪,它可能会更快。 不,我没有使用 XSD 生成类。

我看到有关于使用 Linq 读取和解析 KML 的帖子。 但是,有人使用 Linq to XML 来创建 KML 吗? 如果没有,您认为创建程序的最佳方法是什么。 易于使用一组类来抽象 KML? 使用 Linq to 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-07-21 11:44:34

不是 KML,但这里是使用 System.Xml.Serialization 的示例

给定这两个类(带有属性)和初始化:

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

您可以像这样序列化为 xml:

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

等效的 Linq to Xml 看起来像这样(未测试)

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

结论,两者都比使用 XmlDocument 更简洁,XmlSerializer 更短,Linq to XML 为您提供了更大的灵活性(就类结构与 xml 结构的不同程度而言,XmlSerializer 相当“严格”)。

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

捎一片雪花 2024-07-21 11:44:34

我没有使用过 KML,但 Linq to XML 库本质上是 System.Xml 中 XmlDocument、XmlElement 等模型的替代品。 从内存的角度来看,我不知道它们是否比您当前的解决方案更好。

我经常使用的一种方法是创建一组表示 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 的对象模型(如上面的示例),那么它应该是一个比较简单的实现。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文