C铅XMLノードの属性/属性値を通してXMLの操作コードを書き込んだ例を読みだします。
1.XMLの内容は以下の通りです。
<?xml version="1.0" encoding="utf-8" ?>
<root>
<title>
<settings id = "0" name = " "> ! , ......</settings>
<settings id = "1" name = " "> </settings>
<settings id = "2" name = " ">Yellow</settings>
<settings id = "3" name = " ">48</settings>
</title>
<menu>
<submu id="0" name=" "/>
<submu id="1" name=" "/>
<submu id="2" name=" "/>
<submu id="3" name=" "/>
</menu>
<mu1>
<submu id = "0" name = "iCentroView ">
<video id = "0">Videos/ICV.mp4</video>
</submu>
<submu id = "1" name = " ">
<video id = "0">Videos/ygsqxcp.mp4</video>
</submu>
<submu id = "2" name = " ">
<video id = "0">Videos/iBaosight.mp4</video>
</submu>
<submu id = "1" name = " ">
<video id = "0">Videos/goodlift.mp4</video>
</submu>
</mu1>
<main>Picture/main.jpg</main>
</root>
2.XMLドキュメントを取得する
private static string url = AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"Config\\config.xml";
private XmlDocument xmlDoc;
private XmlNode root;
public static string Title;
public XMLHandler()
{
xmlDoc = new XmlDocument();
LoadConfig();
}
private void LoadConfig()
{
try
{
xmlDoc.Load(url);
root = xmlDoc.SelectSingleNode("root");
}
catch (Exception e)
{
throw e;
}
}
3.属性名からXMLノードの内容を読み取る
public TitleModel GetTitle()
{
try
{
TitleModel title = new TitleModel();
XmlNode node = root.FirstChild;
if(node!=null)
{
foreach (XmlNode nd in node.ChildNodes)
{
XmlElement element = (XmlElement)nd;
if (element.GetAttribute("name") == " ")
{
title.Title = nd.InnerText;
}
else if (element.GetAttribute("name") == " ")
{
title.FontSize = Convert.ToInt32(nd.InnerText);
}
else if (element.GetAttribute("name") == " ")
{
title.FontColor = FontColor(nd.InnerText);
}
else if (element.GetAttribute("name") == " ")
{
title.FontFamily = nd.InnerText;
}
}
}
return title;
}
catch (Exception e)
{
throw e;
}
}
4.XMLのノードの属性値を属性で読み出す
public List<string> GetMenuString()
{
try
{
List<string> list=new List<string>();
XmlNode menu = root.ChildNodes[1];
if (menu != null)
{
foreach (XmlNode node in menu.ChildNodes)
{
XmlElement element = (XmlElement)node;
list.Add(element.GetAttribute("name"));
}
}
return list;
}
catch (Exception e)
{
throw e;
}
}
5.ノードを介してXMLノードの内容を取得する
public string GetMainBackground()
{
string url ="Images/mainjpg";
try
{
XmlNode node = root.LastChild;
if (node != null)
{
url = node.InnerText;
}
return url;
}
catch (Exception e)
{
throw e;
}
}