【问题标题】:XML file to other schema based XML file using c#使用 c# 将 XML 文件转换为其他基于模式的 XML 文件
【发布时间】:2017-11-02 08:45:14
【问题描述】:

是否可以使用 c# 根据其他模式 XSD 重写 XML 文件?

这是 XML 文件

这是当前架构 XSD 文件

这是新的架构,除了更改了节点名称外,其他所有内容都相同

那么如何使用 c# 从基于新 XSD 的旧 XML 中获取新 XML?

【问题讨论】:

    标签: c# xml xsd xml-parsing xsd-validation


    【解决方案1】:

    使用 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")));
    
                }
    
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      相关资源
      最近更新 更多