2011-02-25 79 views
0

我有一個XML DOMC#解析XML Attriubte與名稱與名字b記錄它

<xml> 
<ElementA/> 
<xml> 

現在我的XML改變

<xml> 
<ElementB/> 
<xml> 

ElementB的值的A是simular但我有ElementA的一些文件,並且必須將它們遷移到ElementB。

我與XmlSerializer的

工作是否可以讀取兩個節點的一個參數,並將其寫入到一個屬性名稱,比如

[XmlElement("ElementB")] 
[XmlElement("ElementA")] // Writing this version only somehow 
    public float Rating 
    { get; set; } 
+0

僅供參考 - 您調用「屬性」的內容實際上是*元素*。這兩者在XML中是不同的概念。 – Cheeso 2011-02-27 13:55:13

+0

你是完全正確的,我會改變它 – Markus 2011-03-04 09:33:31

回答

2

我已經使用Linq to XML(System.Xml.Linq)在過去做了這種模式遷移工作,並取得了巨大成功。您的代碼看起來是這樣的:

XDocument doc = XDocument.Load("<path to input xml document>"); 
foreach (var element in doc.Descendants("AttributeA")) 
{ 
    element.Name = "AttributeB"; 
} 
doc.Save("<path to output xml document>"); 

基本上,你剛纔讀在整個文檔中成和的XDocument,發現您是通過LINQ的尋找節點,操縱他們的價值觀,並寫了改變的XDocument回給光盤。如果你仍然想使用Xml序列化,你可以再次從光盤上讀取文檔,並繼續像以前一樣處理。

1

你可以做你想做的與XML屬性覆蓋。實質上, 您可以在運行時將XML序列化屬性應用於序列化程序。

See the MSDN Article

下面是使用XML一些示例代碼屬性重寫與XML序列化。

public class DTO 
{ 
    [XmlIgnore] 
    public string additionalInformation; 

    [XmlElement(Order=1)] 
    public DateTime stamp; 

    [XmlElement(Order=2)] 
    public string name; 

    [XmlElement(Order=3)] 
    public double value; 

    [XmlElement(Order=4)] 
    public int index; 
} 


public class OverridesDemo 
{ 
    public void Run() 
    { 
     DTO dto = new DTO { 
      additionalInformation = "This information will be serialized separately", 
      stamp = DateTime.UtcNow, 
      name = "Marley", 
      value = 72.34, 
      index = 7 
     }; 

     // this will allow us to omit the xmlns:xsi namespace 
     var ns = new XmlSerializerNamespaces(); 
     ns.Add("", ""); 

     XmlSerializer s1 = new XmlSerializer(typeof(DTO)); 

     var builder = new System.Text.StringBuilder(); 
     var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent= true }; 

     Console.WriteLine("\nSerialize using the in-line (compile-time) attributes: "); 
     using (XmlWriter writer = XmlWriter.Create(builder, settings)) 
     { 
      s1.Serialize(writer, dto, ns); 
     } 
     Console.WriteLine("{0}",builder.ToString()); 
     Console.WriteLine("\n"); 


     // use a default namespace 
     ns = new XmlSerializerNamespaces(); 
     string myns = "urn:www.example.org"; 
     ns.Add("", myns); 

     XmlAttributeOverrides overrides = new XmlAttributeOverrides(); 

     XmlAttributes attrs = new XmlAttributes(); 
     // override the (implicit) XmlRoot attribute 
     XmlRootAttribute attr1 = new XmlRootAttribute 
      { 
       Namespace = myns, 
       ElementName = "DTO-Annotations", 
      }; 
     attrs.XmlRoot = attr1; 

     overrides.Add(typeof(DTO), attrs); 
     // "un-ignore" the first property 
     // define an XmlElement attribute, for a type of "String", with no namespace 
     var a2 = new XmlElementAttribute(typeof(String)) { ElementName="note", Namespace = myns }; 

     // add that XmlElement attribute to the 2nd bunch of attributes 
     attrs = new XmlAttributes(); 
     attrs.XmlElements.Add(a2); 
     attrs.XmlIgnore = false; 

     // add that bunch of attributes to the container for the type, and 
     // specifically apply that bunch to the "Label" property on the type. 
     overrides.Add(typeof(DTO), "additionalInformation", attrs); 

     // ignore the other properties 

     // add the XmlIgnore attribute to the 2nd bunch of attributes 
     attrs = new XmlAttributes(); 
     attrs.XmlIgnore = true; 

     // add that bunch of attributes to the container for the type, and 
     // specifically apply that bunch to the "Label" property on the type. 
     overrides.Add(typeof(DTO), "stamp", attrs); 
     overrides.Add(typeof(DTO), "name", attrs); 
     overrides.Add(typeof(DTO), "value", attrs); 
     overrides.Add(typeof(DTO), "index", attrs); 

     XmlSerializer s2 = new XmlSerializer(typeof(DTO), overrides); 

     Console.WriteLine("\nSerialize using the override attributes: "); 
     builder.Length = 0; 
     using (XmlWriter writer = XmlWriter.Create(builder, settings)) 
     { 
      s2.Serialize(writer, dto, ns); 
     } 
     Console.WriteLine("{0}",builder.ToString()); 
     Console.WriteLine("\n"); 
    } 

您可以使用覆蓋進行序列化或反序列化。