xml
xml 有屬性樣子
https://stackoverflow.com/questions/9025288/xmlwriter-to-write-element-string-with-attribute
<book id='1' author='j.k.rowling' year='2010'>
<book>999</book>
</book>
xml 讀取屬性
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlNodeReader reader = null;
try
{
//Create and load the XML document.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861003-78' publicationdate='1987'> " +
"</book>");
// Load the XmlNodeReader
reader = new XmlNodeReader(doc);
//Read the ISBN attribute.
reader.MoveToContent();
string isbn = reader.GetAttribute("ISBN");
Console.WriteLine("The ISBN value: " + isbn);
}
finally
{
if (reader != null)
reader.Close();
}
}
} // End class
OuterXml
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<?xml version='1.0' ?>" +
"<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
//Display the document element.
Console.WriteLine(doc.DocumentElement.OuterXml);
}
}
輸出的樣子
<book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book>
其他:
//取得->網頁所有文字,用StreamReader物件讀取所有字串,儲存到string物件並回傳
string resp = Util.GetURLResp(url);
//字串->XML物件(但是轉XML物件回傳><)
XmlDocument doc = new XmlDocument();
doc.LoadXml(resp);
//把><轉成正常<>
XmlElement root = doc.DocumentElement;
doc.LoadXml(root.InnerText);
//做第二次 XML 的解析
root = doc.DocumentElement;
doc.LoadXml(root.InnerXml);
//reader
XmlNodeReader reader = new XmlNodeReader(doc);
reader.MoveToContent();//(重要)Read the ISBN attribute.
string cash = reader.GetAttribute("cash");
//讀取所有網頁,串成字串
string resp = Util.GetURLResp(url);
//把字串載入成XML物件
XmlDocument doc = new XmlDocument();
doc.LoadXml(resp);
XmlElement root = doc.DocumentElement;
//要把 < 這類的東西,改成 < ,變成正常的文字,所以用 InnerText
doc.LoadXml(root.InnerText);
//做第二次 XML 的解析
root = doc.DocumentElement;
//取值
if (root.HasChildNodes)
{
foreach (XmlNode node in root)
{
ABC += node["ABC"].InnerText + ",";
DDD += node["DDD"].InnerText + ",";
}
ABC = ABC.TrimEnd(',');
DDD = DDD.TrimEnd(',');
}
result = ABC + "#" + DDD;
return result;