Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. No doubt an easy one to you experts.
I have an XML that I need to transform into another XML, taking some of the elements and writing to new elements etc...
I have cut the file down considerably, hopefully enough for you to point me in the right direction.

XML
<?xml version="1.0" encoding="utf-8"?>
<kmsg xmlns:env="http://xml.changedthisbit" xmlns="http://xml.changedthisbit">
  <header>
    <env:envelope>
      <env:payload>ORDER</env:payload>
      <env:cfcompany>01</env:cfcompany>
      <env:service>LIVE</env:service>
      <env:source branch="" endpoint="" machine="" password="" />
      <env:destination branch="0002" endpoint="" machine="" />
    </env:envelope>
  </header>
  <body>
    <Order xmlns="urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01">
      <OrderDate>2012-08-08T09:40:00</OrderDate>
      <Extensions>
        <WebOrder>true</WebOrder>
      </Extensions>
      <OrderLine Action="Add" TypeCode="New" TypeDescription="New Item">
         <Product>
            <ProductId>1234</ProductId>
         </Product>
      </OrderLine>
      <OrderLine Action="Add" TypeCode="New" TypeDescription="New Item">
         <Product>
            <ProductId>9876</ProductId>
         </Product>
      </OrderLine>
    </Order>
   </body>
</kmsg>


I am having trouble simply just outputting the OrderDate. I just get this output.
XML
<?xml version="1.0" encoding="iso-8859-1"?>


I haven't started on the for each for the order lines....

Hoping you will be able to point me in the right direction, (I am filling the swear jar with every attempt I make). I know it must be something simple. Thanks in advance.

What I have tried:

I have this transform:-
XML
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output encoding="iso-8859-1"/>
	<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
	<xsl:template match="/">

	<header>
	</header>
	<body>
		<xsl:value-of select = "kmsg/body/Order/OrderDate"/>
	</body>
	</xsl:template>
</xsl:stylesheet>
Posted
Updated 13-Apr-18 8:43am
v3

1 solution

I reformatted the xml to this:
<?xml version="1.0" encoding="utf-8"?>
  <body>
    <Order>
      <OrderDate>2012-08-08T09:40:00</OrderDate>
      <Extensions>
        <WebOrder>true</WebOrder>
      </Extensions>
      <OrderLine Action="Add" TypeCode="New" TypeDescription="New Item">
         <Product>
            <ProductId>1234</ProductId>
         </Product>
      </OrderLine>
      <OrderLine Action="Add" TypeCode="New" TypeDescription="New Item">
         <Product>
            <ProductId>9876</ProductId>
         </Product>
      </OrderLine>
    </Order>
   </body>

And then ran this code:
XElement _x = XElement.Load(@"order.xml");
var orders = from el in _x.Elements("Order") select el;

foreach (var order in orders)
{
    Debug.Print(order.Element("OrderDate").Value);
}

You could clean up the XML in an automated way like this:
 XDocument doc = XDocument.Load("order.xml");

 // Strip namespace...
 foreach (var node in doc.Root.Descendants()
                         .Where(n => n.Name.NamespaceName == "urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01"))
 {
     // Remove the xmlns='' attribute.
     node.Attributes("xmlns").Remove();
     node.Name = node.Parent.Name.Namespace + node.Name.LocalName;
 }

Debug.Print(doc.ToString());     // Or doc.Save(...)
 
Share this answer
 
v2
Comments
MKM_Matt 16-Apr-18 3:56am    
Hi RickZeeland. Many thanks. I am hoping to have the final solution in XML transform (.xsl) rather than code, i.e. my system will call the transform and load the resulting XML then deal with it. Is there a way to do this? Although will bear your solution in mind if I have to go down the route of doing it all in the code, i.e. read the XML and build the new XML from that.
RickZeeland 16-Apr-18 4:06am    
That seems the way to go, but sadly I have zero experience in XML transform, maybe this will be of help: https://stackoverflow.com/questions/18872689/best-way-to-fix-malformed-html-for-use-in-an-xsl-transform

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900