2015-05-13 81 views
0

我有一個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}));   
    } 

回答

1

嘗試下面的代碼。我用字典來存儲替換值

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 
using System.IO; 

namespace ConsoleApplication28 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string input = 
       "<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\" />"; 

      input = "<Root>" + input + "</Root>"; 
      StringReader reader = new StringReader(input); 
      XDocument doc = XDocument.Load(reader); 

      int value = 1; 
      Dictionary<string, int> dict = new Dictionary<string, int>(); 
      foreach (XElement connection in doc.Root.Elements("Connection")) 
      { 
       string fromAttr = connection.Attribute("From_PhraseID").Value; 
       if (!dict.ContainsKey(fromAttr)) 
       { 
        dict.Add(fromAttr, value++); 
       } 
       string toAttr = connection.Attribute("To_PhraseID").Value; 
       if (!dict.ContainsKey(toAttr)) 
       { 
        dict.Add(toAttr, value++); 
       } 
      } 
      foreach (XElement connection in doc.Root.Elements("Connection")) 
      { 
       string fromAttr = connection.Attribute("From_PhraseID").Value; 
       connection.Attribute("From_PhraseID").Value = dict[fromAttr].ToString(); 
       string toAttr = connection.Attribute("To_PhraseID").Value; 
       connection.Attribute("To_PhraseID").Value = dict[toAttr].ToString(); 
      } 
     } 
    } 
} 
+0

謝謝,那個作品~~~ – Eleanor

0

使用屬性setter?

你被node.Attributes["ConnectionID"].Value所獲得的價值,這樣你就可以將其設置:

node.Attributes["ConnectionID"].Value = "new value" 

我強烈建議使用LINQ to XML(XDocument),而不是舊XmlDocument API如果你能。

在這種情況下,使用XmlSerializer將您的XML反序列化爲Connection對象,進行更改並將其序列化回XML可能更有意義(並使用更少的代碼)。

目前還不清楚你的重編號邏輯是什麼。我創建了一個例子與LINQ這樣對XML和數量它們出現在順序樂句編號:

public class ConnectionRenumberer 
{ 
    private List<string> phraseIds; 

    public string RenumberConnections(string xml) 
    { 
     phraseIds = new List<string>(); 

     var doc = XDocument.Parse(xml); 

     foreach (var connection in doc.Descendants("Connection")) 
     { 
      var from = connection.Attribute("From_PhraseID"); 
      from.Value = NewPhraseId(from.Value); 

      var to = connection.Attribute("To_PhraseID"); 
      to.Value = NewPhraseId(to.Value);      
     } 

     return doc.ToString(); 
    } 

    private string NewPhraseId(string value) 
    { 
     var index = phraseIds.IndexOf(value); 

     if (index == -1) 
     { 
      index = phraseIds.Count; 
      phraseIds.Add(value); 
     } 

     return index.ToString(); 
    } 
} 

這是一個相當幼稚的解決方案。如果輸入的xml很大,phraseIds可以更好地替換爲Dictionary

+0

有意義,所以建立一個「新值」的列表來替換它們,但是我怎樣才能取代正確的?也許你有一個例子? – Eleanor

+0

這是一個與你試圖實現的邏輯有關的不同問題。你問過如何改變這個值;)。因爲它應該很簡單,所以我會用一個使用LINQ to XML的例子。 –

0

我使用LINQ to XML。

這裏是一個樣例,將做你想要的一個元素。我會讓你從那裏拿走它。

static void Main(string[] args) 
     { 
      try 
      { 
       string rootXml = "<Root><Connection ConnectionID=\"106359\" From_PhraseID=\"1\" To_PhraseID=\"tg1c8p-jgg-dbh-b4l-hir5cpla7_nl\"></Connection><Connection ConnectionID=\"106360\" From_PhraseID=\"tg1c8p-jgg-dbh-b4l-hir5cpla7_nl\" To_PhraseID=\"tg0if2-jc8-k6i-spg-2tof46ftr_nl\"></Connection><Connection ConnectionID=\"106361\" From_PhraseID=\"tg1c8p-jgg-dbh-b4l-hir5cpla7_nl\" To_PhraseID=\"4trq50-2h0-kqc-9ku-bm8f4cte7_nl\"></Connection><Connection ConnectionID=\"106362\" From_PhraseID=\"tg1c8p-jgg-dbh-b4l-hir5cpla7_nl\" To_PhraseID=\"1fpspg-tmq-7ln-a9b-3mr3962ca_nl\"></Connection><Connection ConnectionID=\"106358\" From_PhraseID=\"tg0if2-jc8-k6i-spg-2tof46ftr_nl\" To_PhraseID=\"jmrgi1-dst-kt6-roo-lrahuk6tj_nl\"></Connection><Connection ConnectionID=\"106373\" From_PhraseID=\"4trq50-2h0-kqc-9ku-bm8f4cte7_nl\" To_PhraseID=\"97bngg-ggb-k8l-ggf-qnre46ckq_nl\"></Connection><Connection ConnectionID=\"106376\" From_PhraseID=\"1fpspg-tmq-7ln-a9b-3mr3962ca_nl\" To_PhraseID=\"bqgccm-55n-iur-061-27obhegve_nl\"></Connection></Root>"; 

       XElement root = XElement.Load((new StringReader(rootXml)), LoadOptions.None); 

       root.Descendants().Where(i => i.Attribute("To_PhraseID").Value == "tg1c8p-jgg-dbh-b4l-hir5cpla7_nl") 
            .ToList() 
            .ForEach(i => i.SetAttributeValue("To_PhraseID", "MYNEWID")); 

       var x = root.ToString(); 
      } 
      catch (Exception ex) 
      { 

      } 
     } 
+0

當我調用這個函數,ReplaceProductCodeInXML(字符串xml,字符串nodeToUpdate,字符串newValue)時,我應該在新值中放入什麼,如果我只放一個字符串,所有新對象將具有相同的值...對嗎? – Eleanor