本文整理汇总了C#中XmlDocument类的典型用法代码示例。如果您正苦于以下问题:C# XmlDocument类的具体用法?C# XmlDocument怎么用?C# XmlDocument使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XmlDocument类属于命名空间,在下文中一共展示了XmlDocument类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAttributesOnTextNode
public static void GetAttributesOnTextNode()
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<a>text node</a>");
Assert.Null(xmlDocument.DocumentElement.FirstChild.Attributes);
}
示例2: Main
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"..\..\..\catalogue.xml");
XmlNode rootNode = doc.DocumentElement;
for (int i = 0; i < rootNode.ChildNodes.Count; i++)
{
XmlNode node = rootNode.ChildNodes[i];
foreach (XmlNode albumPrice in node.ChildNodes)
{
// if the node is the price node
if (albumPrice.Name == "price")
{
// take the price
double price = double.Parse(albumPrice.InnerText);
if (price > 20.00)
{
rootNode.RemoveChild(node); // remove the node
i--; // decrementing i so that we won't miss a node
}
}
}
}
doc.Save(@"..\..\..\catalogueUnderTwentyPrice.xml");
}
示例3: SetAppTypeProduct
public void SetAppTypeProduct()
{
try
{
XmlDocument doc = new XmlDocument();
string xpath = Server.MapPath("../data/xml/configproduct.xml");
XmlTextReader reader = new XmlTextReader(xpath);
doc.Load(reader);
reader.Close();
XmlNodeList nodes = doc.SelectNodes("/root/product");
int numnodes = nodes.Count;
for (int i = 0; i < numnodes; i++)
{
string nameapp = nodes.Item(i).ChildNodes[0].InnerText;
string idtype = nodes.Item(i).ChildNodes[1].InnerText;
string appunit = nodes.Item(i).ChildNodes[2].InnerText;
string unit = nodes.Item(i).ChildNodes[3].InnerText;
if (nameapp.Length > 0 && idtype.Length > 0)
{
Application[nameapp] = int.Parse(idtype);
}
if (appunit.Length > 0 && unit.Length > 0)
{
Application[appunit] = unit;
}
}
}
catch
{
}
}
示例4: DocumentNodeTypeTest
public static void DocumentNodeTypeTest()
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<a />");
Assert.Equal(XmlNodeType.Document, xmlDocument.NodeType);
}
示例5: GetUpdateList
private void GetUpdateList(string updateKey)
{
WebClient client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string response;
try
{
response = client.UploadString("http://infinity-code.com/products_update/checkupdates.php",
"k=" + WWW.EscapeURL(updateKey) + "&v=" + OnlineMaps.version + "&c=" + (int)channel);
}
catch
{
return;
}
XmlDocument document = new XmlDocument();
document.LoadXml(response);
XmlNode firstChild = document.FirstChild;
updates = new List<OnlineMapsUpdateItem>();
foreach (XmlNode node in firstChild.ChildNodes) updates.Add(new OnlineMapsUpdateItem(node));
}
示例6: SetCountriesName
public static void SetCountriesName(string language)
{
Debug.Log("countries....." + language);
TextAsset textAsset = (TextAsset) Resources.Load("countries");
var xml = new XmlDocument ();
xml.LoadXml (textAsset.text);
Countries = new Hashtable();
AppCountries = new SortedList();
try{
var element = xml.DocumentElement[language];
if (element != null) {
var elemEnum = element.GetEnumerator();
while (elemEnum.MoveNext()) {
var xmlItem = (XmlElement)elemEnum.Current;
Countries.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
AppCountries.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
}
} else {
Debug.LogError("The specified language does not exist: " + language);
}
}
catch (Exception ex)
{
Debug.Log("Language:SetCountryName()" + ex.ToString());
}
}
示例7: GetData
public void GetData()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(LevelStats.text);
StartCoroutine(LoadLevelStats(xmlDoc));
}
示例8: TestOASIS
public void TestOASIS ()
{
XmlDocument doc = new XmlDocument ();
doc.NodeInserting += new XmlNodeChangedEventHandler (OnInserting);
doc.NodeInserted += new XmlNodeChangedEventHandler (OnInserted);
doc.NodeChanging += new XmlNodeChangedEventHandler (OnChanging);
doc.NodeChanged += new XmlNodeChangedEventHandler (OnChanged);
doc.NodeRemoving += new XmlNodeChangedEventHandler (OnRemoving);
doc.NodeRemoved += new XmlNodeChangedEventHandler (OnRemoved);
foreach (FileInfo fi in
new DirectoryInfo (@"xml-test-suite/xmlconf/oasis").GetFiles ("*.xml")) {
try {
if (fi.Name.IndexOf ("fail") >= 0)
continue;
Console.WriteLine ("#### File: " + fi.Name);
XmlTextReader xtr = new XmlTextReader (fi.FullName);
xtr.Namespaces = false;
xtr.Normalization = true;
doc.RemoveAll ();
doc.Load (xtr);
} catch (XmlException ex) {
if (fi.Name.IndexOf ("pass") >= 0)
Console.WriteLine ("Incorrectly invalid: " + fi.FullName + "\n" + ex.Message);
}
}
}
示例9: Adjust
public void Adjust(string fileName)
{
try
{
string path = Path.GetDirectoryName(Application.dataPath+"..");
if (File.Exists(path+"/"+fileName))
{
TextReader tx = new StreamReader(path+"/"+fileName);
if (doc == null)
doc = new XmlDocument();
doc.Load(tx);
tx.Close();
if (doc!=null && doc.DocumentElement!=null)
{
string xmlns = doc.DocumentElement.Attributes["xmlns"].Value;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("N",xmlns);
SetNode("TargetFrameworkVersion","v4.0", nsmgr);
// SetNode("DefineConstants","TRACE;UNITY_3_5_6;UNITY_3_5;UNITY_EDITOR;ENABLE_PROFILER;UNITY_STANDALONE_WIN;ENABLE_GENERICS;ENABLE_DUCK_TYPING;ENABLE_TERRAIN;ENABLE_MOVIES;ENABLE_WEBCAM;ENABLE_MICROPHONE;ENABLE_NETWORK;ENABLE_CLOTH;ENABLE_WWW;ENABLE_SUBSTANCE", nsmgr);
TextWriter txs = new StreamWriter(path+"/"+fileName);
doc.Save(txs);
txs.Close();
}
}
}
catch(System.Exception)
{
}
}
示例10: Main
static void Main()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("../../catalogue.xml");
string xPathQuery = "/catalog/album";
XmlNodeList ArtistsList = xmlDoc.SelectNodes(xPathQuery);
Dictionary<string, int> artistsAndAlbumsCount = new Dictionary<string, int>();
foreach (XmlNode ArtistNode in ArtistsList)
{
string authorName = ArtistNode.SelectSingleNode("artist").InnerText;
if (artistsAndAlbumsCount.ContainsKey(authorName))
{
artistsAndAlbumsCount[authorName]++;
}
else
{
artistsAndAlbumsCount.Add(authorName, 1);
}
}
foreach (var artist in artistsAndAlbumsCount)
{
Console.WriteLine("Artist: {0}", artist.Key);
Console.WriteLine("Albums count: {0}", artist.Value);
Console.WriteLine();
}
}
示例11: DeleteAttribute
/// <summary>
/// 删除XmlNode属性
/// 使用示列:
/// XmlHelper.DeleteAttribute(path, nodeName, "");
/// XmlHelper.DeleteAttribute(path, nodeName, attributeName,attributeValue);
/// </summary>
/// <param name="path">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="attributeName">属性名称</param>
/// <returns>void</returns>
public static void DeleteAttribute(string path, string nodeName, string attributeName)
{
if (attributeName.Equals(""))
{
return;
}
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement element = doc.SelectSingleNode(nodeName) as XmlElement;
if (element == null)
{
throw new Exception("节点元素不存在!");
}
else
{
element.RemoveAttribute(attributeName);
doc.Save(path);
}
}
catch
{ }
}
示例12: WriteToXml
public static void WriteToXml(string namePlayer)
{
string filepath = Application.dataPath + @"/Data/hightScores.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(filepath))
{
xmlDoc.Load(filepath);
XmlElement elmRoot = xmlDoc.DocumentElement;
// elmRoot.RemoveAll(); // remove all inside the transforms node.
XmlElement scoresHelper = xmlDoc.CreateElement("allScores"); // create the rotation node.
XmlElement name = xmlDoc.CreateElement("name"); // create the x node.
name.InnerText = namePlayer; // apply to the node text the values of the variable.
XmlElement score = xmlDoc.CreateElement("score"); // create the x node.
score.InnerText = "" + Game.points; // apply to the node text the values of the variable.
scoresHelper.AppendChild(name); // make the rotation node the parent.
scoresHelper.AppendChild(score); // make the rotation node the parent.
elmRoot.AppendChild(scoresHelper); // make the transform node the parent.
xmlDoc.Save(filepath); // save file.
}
}
示例13: GetAttributesOnDocumentFragment
public static void GetAttributesOnDocumentFragment()
{
var xmlDocument = new XmlDocument();
var documentFragment = xmlDocument.CreateDocumentFragment();
Assert.Null(documentFragment.Attributes);
}
示例14: GetAttributesOnCDataNode
public static void GetAttributesOnCDataNode()
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<a><![CDATA[test data]]></a>");
Assert.Null(xmlDocument.DocumentElement.FirstChild.Attributes);
}
示例15: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
//Load document
string booksFile = Server.MapPath("books.xml");
XmlDocument document = new XmlDocument();
document.Load(booksFile);
XPathNavigator nav = document.CreateNavigator();
//Add a namespace prefix that can be used in the XPath expression
XmlNamespaceManager namespaceMgr = new XmlNamespaceManager(nav.NameTable);
namespaceMgr.AddNamespace("b", "http://example.books.com");
//All books whose price is not greater than 10.00
foreach (XPathNavigator node in
nav.Select("//b:book[not(b:price[. > 10.00])]/b:price",
namespaceMgr))
{
Decimal price = (decimal)node.ValueAs(typeof(decimal));
node.SetTypedValue(price * 1.2M);
Response.Write(String.Format("Price raised from {0} to {1}<BR/>",
price,
node.ValueAs(typeof(decimal))));
}
}