我有一個xml,我想用C#重寫一遍,用不同的vaule到transfrom吧,這裏是老的xml:用C#替換XML值
<Connection ConnectionID="106359" From_PhraseID="1" To_PhraseID="tg1c8p-jgg-dbh-b4l-hir5cpla7_nl" />
<Connection ConnectionID="106360" From_PhraseID="tg1c8p-jgg-dbh-b4l-hir5cpla7_nl" To_PhraseID="tg0if2-jc8-k6i-spg-2tof46ftr_nl" />
<Connection ConnectionID="106361" From_PhraseID="tg1c8p-jgg-dbh-b4l-hir5cpla7_nl" To_PhraseID="4trq50-2h0-kqc-9ku-bm8f4cte7_nl" />
<Connection ConnectionID="106362" From_PhraseID="tg1c8p-jgg-dbh-b4l-hir5cpla7_nl" To_PhraseID="1fpspg-tmq-7ln-a9b-3mr3962ca_nl" />
<Connection ConnectionID="106358" From_PhraseID="tg0if2-jc8-k6i-spg-2tof46ftr_nl" To_PhraseID="jmrgi1-dst-kt6-roo-lrahuk6tj_nl" />
<Connection ConnectionID="106373" From_PhraseID="4trq50-2h0-kqc-9ku-bm8f4cte7_nl" To_PhraseID="97bngg-ggb-k8l-ggf-qnre46ckq_nl" />
<Connection ConnectionID="106376" From_PhraseID="1fpspg-tmq-7ln-a9b-3mr3962ca_nl" To_PhraseID="bqgccm-55n-iur-061-27obhegve_nl" />
,並認爲是新的XML:
<Connection ConnectionID="106359" From_PhraseID="0" To_PhraseID="1" />
<Connection ConnectionID="106360" From_PhraseID="1" To_PhraseID="2" />
<Connection ConnectionID="106361" From_PhraseID="1" To_PhraseID="3" />
<Connection ConnectionID="106362" From_PhraseID="1" To_PhraseID="4" />
<Connection ConnectionID="106358" From_PhraseID="2" To_PhraseID="6" />
<Connection ConnectionID="106373" From_PhraseID="3" To_PhraseID="7" />
<Connection ConnectionID="106376" From_PhraseID="4" To_PhraseID="5" />
任何想法如何把它寫在C#中,我在這裏有一個開始,這個代碼可以加載舊的xml獲得在C#中所有的舊價值,但如何改變價值
public class Dialogs{
public Dictionary<string, Connection> Connections = new Dictionary<string, Connection>();
}
public class Connection
{
public string ConnectionID = null;
public string FromPhraseID = null;
public string ToPhraseID = null;
}
List<Connection> connectionsList = new List<Connection>();
Dialogs resultDialog = new Dialogs();
// Document
XmlDocument xml = new XmlDocument();
TextAsset asset = (TextAsset)Resources.Load("Dialogs/online");
xml.LoadXml(asset.text);
// Connections
XmlNodeList connections = xml.GetElementsByTagName("Connection");
foreach (XmlNode connectionNode in connections)
{
Connection connection = NodeToConnection(connectionNode);
connectionsList.Add(connection);
if (connection != null)
AddOrUpdateList(resultDialog.FromLists, connection);
}
private Connection NodeToConnection(XmlNode node)
{
Connection connection = new Connection();
connection.ConnectionID = node.Attributes["ConnectionID"].Value;
connection.FromPhraseID = node.Attributes["From_PhraseID"].Value;
connection.ToPhraseID = node.Attributes["To_PhraseID"].Value;
return connection;
}
private void AddOrUpdateList(Dictionary<string, List<Connection>> dic, Connection con)
{
if (dic.ContainsKey(con.FromPhraseID))
dic[con.FromPhraseID].Add(con);
else
dic.Add(con.FromPhraseID, new List<Connection>(new Connection[] {con}));
}
謝謝,那個作品~~~ – Eleanor