CSharp · Console
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
// https://stackoverflow.com/questions/48913782/transform-style-xml-in-c-sharp-with-xmldocument?noredirect=1#comment84836176_48913782
static class TestClass
{
public static void Test()
{
// Load the XML
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(GetXml());
Console.WriteLine("\nInitial XML:");
Console.WriteLine(xmlDoc.GetOuterXml());
// Get the XML namespace of the root element.
var namespaceURI = xmlDoc.DocumentElement.NamespaceURI;
// Create the subsystem in the same XML namespace
var subsystem = xmlDoc.DocumentElement.AppendChild(xmlDoc.CreateElement("subsystem", namespaceURI));
// Add the specified attributes.
subsystem.Attributes.Append(xmlDoc.CreateAttributeWithValue("name", "hahaha"));
subsystem.Attributes.Append(xmlDoc.CreateAttributeWithValue("tag1", "NoNo"));
subsystem.Attributes.Append(xmlDoc.CreateAttributeWithValue("tag2", "SoNo"));
// Save the XML
Console.WriteLine("\nModified XML:");
Console.WriteLine(xmlDoc.GetOuterXml());
}
static string GetXml()
{
var xml = @"<?xml version=""1.0""?>
<chssystem ExportDate=""2/21/2018"" ExportTime=""2:57 PM EST"" DateFormat=""MM/dd/yyyy"" NumberFormat=""HH:mm:ss "" SchemaValidation=""true"" ExportVersion=""2016.1.SP1710.57"" xmlns=""http://www.mentor.com/harness/Schema/LibrarySchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://www.mentor.com/harness/Schema/LibrarySchema file:/C:/MentorGraphics/VeSys_Client/dtd/LibrarySchema.xsd"" XMLVersion=""1.6"">
<librarycomponenttype librarycomponenttype_id=""_GROUPID_CONNECTOR"" description=""Connector"" clipgromident="""" typecode=""CONN"" />
<connectorpart libraryobject_id=""_OID_CTEST"" depth=""0.0"" description=""TEST DATA"" graphics=""0"" groupname=""Connector"" librarycomponenttype_id=""_GROUPID_CONNECTOR"" cavityqt=""1"" partnumber=""CONN-C-TEST"" unitofmeasure=""Each"" >
<librarycavity librarycavity_id=""ID1"" ca_mappingtype=""Userdefined"" ca_attach="" "" isblocked=""1"" pingraphic="" "" cavityname=""1"" librarypincontainer_id=""_OID_CTEST"" sortorder=""1""/>
</connectorpart>
</chssystem>";
return xml;
}
}
public static class XmlNodeExtensions
{
public static XmlAttribute CreateAttributeWithValue(this XmlDocument doc, string name, string value)
{
var attr = doc.CreateAttribute(name);
attr.Value = value;
return attr;
}
}
public static class XmlSerializationHelper
{
public static string GetOuterXml(this XmlNode node, bool indent = true)
{
if (node == null)
return null;
using (var textWriter = new StringWriter())
{
var settings = new XmlWriterSettings
{
Indent = indent,
OmitXmlDeclaration = true,
};
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
{
node.WriteTo(xmlWriter);
}
return textWriter.ToString();
}
}
}
public class AssertionFailedException : System.Exception
{
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
}
public static class Assert
{
public static void IsTrue(bool value)
{
IsTrue(value, "failed");
}
public static void IsTrue(bool value, string message)
{
if (value == false)
throw new AssertionFailedException(message ?? "failed");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Uncaught exception: ");
Console.WriteLine(ex);
throw;
}
}
}