Следующий фрагмент кода приведет к ошибке компилятора (VC2010): C2248: «base :: ~ base»: не может получить доступ к частному члену, объявленному в классе «base»
class base
{
~base(){}
};
class derived : public base
{
};
int main ()
{
derived* d = new derived;
delete d;
}
Однако, если вы измените базовый деструктор, который будет защищен, все будет хорошо.
If you're using .NET version 3.0 or lower, you have to use XmlDocument
aka the classic DOM API. Likewise you'll find there are some other APIs which will expect this.
If you get the choice, however, I would thoroughly recommend using XDocument
aka LINQ to XML. It's much simpler to create documents and process them. For example, it's the difference between:
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("root");
root.SetAttribute("name", "value");
XmlElement child = doc.CreateElement("child");
child.InnerText = "text node";
root.AppendChild(child);
doc.AppendChild(root);
and
XDocument doc = new XDocument(
new XElement("root",
new XAttribute("name", "value"),
new XElement("child", "text node")));
Namespaces are pretty easy to work with in LINQ to XML, unlike any other XML API I've ever seen:
XNamespace ns = "http://somewhere.com";
XElement element = new XElement(ns + "elementName");
// etc
LINQ to XML also works really well with LINQ - its construction model allows you to build elements with sequences of sub-elements really easily:
// Customers is a List<Customer>
XElement customersElement = new XElement("customers",
customers.Select(c => new XElement("customer",
new XAttribute("name", c.Name),
new XAttribute("lastSeen", c.LastOrder)
new XElement("address",
new XAttribute("town", c.Town),
new XAttribute("firstline", c.Address1),
// etc
));
It's all a lot more declarative, which fits in with the general LINQ style.
Now as Brannon mentioned, these are in-memory APIs rather than streaming ones (although XStreamingElement
supports lazy output). XmlReader
and XmlWriter
are the normal ways of streaming XML in .NET, but you can mix all the APIs to some extent. For example, you can stream a large document but use LINQ to XML by positioning an XmlReader
at the start of an element, reading an XElement
from it and processing it, then moving on to the next element etc. There are various blog posts about this technique, here's one I found with a quick search.
XmlDocument
is great for developers who are familiar with the XML DOM object model. It's been around for a while, and more or less corresponds to a W3C standard. It supports manual navigation as well as XPath
node selection.
XDocument
powers the LINQ to XML feature in .NET 3.5. It makes heavy use of IEnumerable<>
and can be easier to work with in straight C#.
Both document models require you to load the entire document into memory (unlike XmlReader
for example).
XDocument
взят из LINQ to XML API, а XmlDocument
- это стандартный API в стиле DOM для XML. Если вы хорошо знаете DOM и не хотите изучать LINQ to XML, воспользуйтесь XmlDocument
. Если вы новичок в обоих, просмотрите эту страницу , на которой сравниваются эти две, и выберите, какая из них вам больше нравится.
Я только начал использовать LINQ to XML, и мне очень нравится способ создания XML-документа с использованием функциональной конструкции. Это действительно приятно. По сравнению с этим модель DOM неуклюжа.