記幾個筆記
檔案后綴不一定要.xml,可以是任意后綴,只要內容是符合xml和格式即可決議出來
檔案編碼需要是utf-8,python和c#都需要,或者xml檔案頭有這樣一句:<?xml version="1.0" encoding="utf-8"?>
一些比較復雜的檔案,如果按照從上往下一層一層節點來決議,那么比較麻煩,但是通過xpath,指定節點來決議,那么就方便多了,
xml檔案示例
<root>
<space>
<name> 江南 </name>
<subzone>
<zone>
<name> 桃溪 </name>
<ambient> jiangnan/forest_05 </ambient>
<music> Zone/xxxx1 </music>
</zone>
<zone>
<name> 木瀆鎮 </name>
<ambient> jiangnan/muduzheng </ambient>
<music> Jn_2/xxxx2 </music>
</zone>
<!--....還有很多zone !-->
<subzone>
<music> Space/Jiangnan_xxx </music>
<ambient> function/amb_wind </ambient>
<space>
</root>
C#版本決議
重要的點就是通過node.SelectSingleNode來選擇要的節點,
var xmldoc = new XmlDocument();
var xmlReader = XmlReader.Create(def_path);
xmldoc.Load(xmlReader);
var nodeList = xmldoc.FirstChild.ChildNodes;
Console.WriteLine($"讀入xml,節點數:{nodeList.Count}");
StringBuilder builder = new StringBuilder();
builder.AppendLine($"場景chunk名稱,音樂名,環境底噪,所屬大區"); //場景chunk名稱 檔案夾名 資源名
foreach (XmlNode node in nodeList)
{
if (node.Name != "space")
{
Console.WriteLine("遇到特殊節點,未決議");
continue;
}
var subzone = node.SelectSingleNode("subzone");
var name = node.SelectSingleNode("name");
var music = node.SelectSingleNode("music");
var ambient = node.SelectSingleNode("ambient");
//....處理第1層節點的資料
if (subzone != null)
{
var zones = subzone.ChildNodes;
foreach (XmlLinkedNode ccNode in zones)
{
var name1 = ccNode.SelectSingleNode("name");
var music1 = ccNode.SelectSingleNode("music");
var ambient1 = ccNode.SelectSingleNode("ambient");
var subzone1 = ccNode.SelectSingleNode("subzone");
//..... 處理第2層節點的資料
//note 如果還有子子節點的話,需要再次處理
if (subzone1 != null)
{
Console.WriteLine($"第2層之后還有subzone, name:{tunkName} ,未決議");
}
}
}
}
python版本決議
python決議xml的檔案:https://docs.python.org/zh-cn/2.7/library/xml.etree.elementtree.html
示例:
import xml.etree.ElementTree as ET
if __name__ == '__main__':
xml_path = r"D:\xxx\scene.xml"
tree = ET.parse(xml_path)
root = tree.getroot()
for child in root:
childs = child.getchildren()
print(childs,child.tag,child.text)
if child.tag == "space" and child.text and child.text:
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/424838.html
標籤:C#
上一篇:獲取所有以某個字串開頭的游戲物件
