【发布时间】:2017-11-02 08:45:14
【问题描述】:
是否可以使用 c# 根据其他模式 XSD 重写 XML 文件?
这是 XML 文件
这是新的架构,除了更改了节点名称外,其他所有内容都相同
那么如何使用 c# 从基于新 XSD 的旧 XML 中获取新 XML?
【问题讨论】:
标签: c# xml xsd xml-parsing xsd-validation
是否可以使用 c# 根据其他模式 XSD 重写 XML 文件?
这是 XML 文件
这是新的架构,除了更改了节点名称外,其他所有内容都相同
那么如何使用 c# 从基于新 XSD 的旧 XML 中获取新 XML?
【问题讨论】:
标签: c# xml xsd xml-parsing xsd-validation
使用 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")));
}
}
}
}
【讨论】: