本文整理汇总了C#中XmlDocument.PrependChild方法的典型用法代码示例。如果您正苦于以下问题:C# XmlDocument.PrependChild方法的具体用法?C# XmlDocument.PrependChild怎么用?C# XmlDocument.PrependChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类XmlDocument
的用法示例。
在下文中一共展示了XmlDocument.PrependChild方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SavePlistToFile
public static bool SavePlistToFile(String xmlFile, Hashtable plist)
{
// If the hashtable is null, then there's apparently an issue; fail out.
if (plist == null) {
Debug.LogError("Passed a null plist hashtable to SavePlistToFile.");
return false;
}
// Create the base xml document that we will use to write the data
XmlDocument xml = new XmlDocument();
xml.XmlResolver = null; //Disable schema/DTD validation, it's not implemented for Unity.
// Create the root XML declaration
// This, and the DOCTYPE, below, are standard parts of a XML property list file
XmlDeclaration xmldecl = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
xml.PrependChild(xmldecl);
// Create the DOCTYPE
XmlDocumentType doctype = xml.CreateDocumentType("plist", "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);
xml.AppendChild(doctype);
// Create the root plist node, with a version number attribute.
// Every plist file has this as the root element. We're using version 1.0 of the plist scheme
XmlNode plistNode = xml.CreateNode(XmlNodeType.Element, "plist", null);
XmlAttribute plistVers = (XmlAttribute)xml.CreateNode(XmlNodeType.Attribute, "version", null);
plistVers.Value = "1.0";
plistNode.Attributes.Append(plistVers);
xml.AppendChild(plistNode);
// Now that we've created the base for the XML file, we can add all of our information to it.
// Pass the plist data and the root dict node to SaveDictToPlistNode, which will write the plist data to the dict node.
// This function will itterate through the hashtable hierarchy and call itself recursively for child hashtables.
if (!SaveDictToPlistNode(plistNode, plist)) {
// If for some reason we failed, post an error and return false.
Debug.LogError("Failed to save plist data to root dict node: " + plist);
return false;
} else { // We were successful
// Create a StreamWriter and write the XML file to disk.
// (do not append and UTF-8 are default, but we're defining it explicitly just in case)
StreamWriter sw = new StreamWriter(xmlFile, false, System.Text.Encoding.UTF8);
xml.Save(sw);
sw.Close();
}
// We're done here. If there were any failures, they would have returned false.
// Return true to indicate success.
return true;
}
示例2: TestXmlDocumentAddElement
// Test adding an element to the document.
public void TestXmlDocumentAddElement()
{
XmlDocument doc = new XmlDocument();
// Add an element to the document.
XmlElement element = doc.CreateElement("foo");
AssertNull("XmlElement (1)", element.ParentNode);
AssertEquals("XmlElement (2)", doc, element.OwnerDocument);
doc.AppendChild(element);
AssertEquals("XmlElement (3)", doc, element.ParentNode);
AssertEquals("XmlElement (4)", doc, element.OwnerDocument);
// Try to add it again, which should fail this time.
try
{
doc.AppendChild(element);
Fail("adding XmlElement node twice");
}
catch(InvalidOperationException)
{
// Success
}
try
{
doc.PrependChild(element);
Fail("prepending XmlElement node twice");
}
catch(InvalidOperationException)
{
// Success
}
// Adding an XmlDeclaration after should fail.
XmlDeclaration decl =
doc.CreateXmlDeclaration("1.0", null, null);
try
{
doc.AppendChild(decl);
Fail("appending XmlDeclaration after XmlElement");
}
catch(InvalidOperationException)
{
// Success
}
// But adding XmlDeclaration before should succeed.
doc.PrependChild(decl);
// Adding a document type after should fail.
XmlDocumentType type =
doc.CreateDocumentType("foo", null, null, null);
try
{
doc.AppendChild(type);
Fail("appending XmlDocumentType");
}
catch(InvalidOperationException)
{
// Success
}
// Adding a document type before should succeed.
doc.InsertBefore(type, element);
}
示例3: TestXmlDocumentAddXmlDeclaration
// Test adding an XML declaration to the document.
public void TestXmlDocumentAddXmlDeclaration()
{
XmlDocument doc = new XmlDocument();
// Add the declaration.
XmlDeclaration decl =
doc.CreateXmlDeclaration("1.0", null, null);
AssertNull("XmlDeclaration (1)", decl.ParentNode);
AssertEquals("XmlDeclaration (2)", doc, decl.OwnerDocument);
doc.AppendChild(decl);
AssertEquals("XmlDeclaration (3)", doc, decl.ParentNode);
AssertEquals("XmlDeclaration (4)", doc, decl.OwnerDocument);
// Try to add it again, which should fail this time.
try
{
doc.AppendChild(decl);
Fail("adding XmlDeclaration node twice");
}
catch(InvalidOperationException)
{
// Success
}
try
{
doc.PrependChild(decl);
Fail("prepending XmlDeclaration node twice");
}
catch(InvalidOperationException)
{
// Success
}
// Adding a document type before should fail.
XmlDocumentType type =
doc.CreateDocumentType("foo", null, null, null);
try
{
doc.PrependChild(type);
Fail("prepending XmlDocumentType");
}
catch(InvalidOperationException)
{
// Success
}
// Adding a document type after should succeed.
doc.AppendChild(type);
// Adding an element before should fail.
XmlElement element = doc.CreateElement("foo");
try
{
doc.PrependChild(element);
Fail("prepending XmlElement");
}
catch(InvalidOperationException)
{
// Success
}
// Adding the element between decl and type should fail.
try
{
doc.InsertAfter(element, decl);
Fail("inserting XmlElement between XmlDeclaration " +
"and XmlDocumentType");
}
catch(InvalidOperationException)
{
// Success
}
try
{
doc.InsertBefore(element, type);
Fail("inserting XmlElement between XmlDeclaration " +
"and XmlDocumentType (2)");
}
catch(InvalidOperationException)
{
// Success
}
// Adding an element after should succeed.
doc.AppendChild(element);
}