2017-11-02 2 views
-1

ist es möglich, XML-Datei nach anderen Schema XSD mit C# neu schreiben?XML-Datei in andere schemabasierte XML-Datei mit C#

dies ist XML-Datei

this is XML file

Dies ist die aktuelle Schema XSD-Datei current schema XSD file

und das ist das neue Schema gleicht alles, aber geänderte Knotennamen

new schema

also, wie man ein neues XML von bekommt der alte basierend auf dem neuen XSD mit C#?

Antwort

0

Mit xml linq:

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


namespace ConsoleApplication13 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 

      List<XElement> shipTo = doc.Descendants("shipto").ToList(); 

      foreach (XElement ship in shipTo) 
      { 
       ship.Element("name").ReplaceWith(new XElement("FullName", (string)ship.Element("name"))); 
       ship.Element("address").ReplaceWith(new XElement("FirstAddress", (string)ship.Element("address"))); 
       ship.Element("city").ReplaceWith(new XElement("homeTown", (string)ship.Element("city"))); 
       ship.Element("country").ReplaceWith(new XElement("HomeLand", (string)ship.Element("country"))); 

      } 

     } 

    } 

} 
Verwandte Themen