I am trying to make some additions to an XML file. I am adding the XML in one format (Format2 below) and would like to transform it to another (Format1 below). How can I do that?
Details:
I have an template.xml
which I use to create file so I can import it to a special application which only takes xml
by updating the existing attributes. However, depending on some cases more elements and attributes need to be added.
A simplified example of the XML is as follows:
<?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>
Let's say I want to add this Format1
<subsystem name="hahaha"
tag1="NoNo"tag2="SoNo" />
Here is the c# code I am using to create this element:
XmlElement subsystem = xmlDoc.CreateElement("subsystem");
XmlElement name= xmlDoc.CreateElement("name");
name.InnerText = "hahaha";
XmlElement tag1= xmlDoc.CreateElement("tag1");
tag1.InnerText = "NoNo";
XmlElement tag2= xmlDoc.CreateElement("tag2");
tag2.InnerText = "SoNo";
After I append them, I have a new XML with the following Format2
<subsystem xmlns="">
<name>hahaha</name>
<tag1>NoNo</tag1>
<tag2>SoNo</tag2>
</subsystem >
So the C# code adds the extra xmlns=""
The application still returns error in both case with or without the extra xmlns=""
If I manually adjust the data to Format1, there is no error ~~~~> If I can transform the Format2 to Format1, I think the application will accept the xml
The error from an application:
In case I keep the xmlns=""
, error message is
Invalid content was found starting with element subsystem, the element name is needed
In case I remove the xmlns=""
, error message is
element name must appear on subsystem, element tag1 must appear on subsystem, element tag2 must appear on subsystem