Remove XML declaration using msxml6/IXMLDOMDocument2 - com

I want to remove XML declaration only from an XML using C++/COM-
<?xml version="1.0" encoding="UTF-8" ?>
using the IXMLDOMDocument2 interface of msxml6
How can I achieve this ?

Use IXMLDocument::xml property and save the string to a file.

Related

How to create a message containing xml prolog declaration

I am trying to create some dynamic xml messages which will be sent to a service which expects xml. Said service also needs the xml prolog to be specified.
As I want to do a lot of manipulation on the xml I am reading it in a native xml type, however it seems that karate loses the xml prolog information during the conversion (on 0.9.5 and 0.9.6.RC3)
Feature: Example
# https://www.w3.org/TR/xml/#sec-prolog-dtd
Scenario: Example
Given def nativeXML =
"""
<?xml version="1.0" encoding="UTF-8" ?>
<greeting>Hello world</greeting>
"""
When string nativeToString = nativeXML
* print nativeToString
Then assert nativeToString.contains("encoding")
Is there a way to preserve the xml prolog?
Code to reproduce this can be found here: https://github.com/KostasKgr/karate-issues/blob/xml_prolog_to_string/src/test/java/examples/example.feature
Kind regards
Right now there is no other way but to append it manually, which IMO is simple enough. Karate removes it by default during a string conversion, because it gets in the way during Node operations.
Given def temp =
"""
<?xml version="1.0" encoding="UTF-8" ?>
<greeting>Hello world</greeting>
"""
* string body = temp
* def body = '<?xml version="1.0" encoding="UTF-8" ?>' + body
* print body
Then assert body.contains("encoding")

reading xml file with namespace

we need to read a xml file in sql server but we are having problems because the xml have a namespace, I have tried several solutions but I can't resolve the problem.
the xml file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<Status:orders xmlns:Status="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com Status.xsd">
<order>
<Header>
<Name>500039</Name>
<Letter>A</Letter>
</Header>
</order>
</Status:orders>
can you help how to retrieve the values for the Name and letter tags
thanks in advance.
Your friend is called WITH XMLNAMESPACES...
Try it like this
DECLARE #xml XML=
'<?xml version="1.0" encoding="UTF-8"?>
<Status:orders xmlns:Status="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com Status.xsd">
<order>
<Header>
<Name>500039</Name>
<Letter>A</Letter>
</Header>
</order>
</Status:orders>';
WITH XMLNAMESPACES('http://www.test.com' AS Status)
SELECT #xml.value('(/Status:orders/order/Header/Name)[1]','int')
,#xml.value('(/Status:orders/order/Header/Letter)[1]','varchar(max)');
An alternative was, to use the asterisk.
SELECT #xml.value('(/*:orders/order/Header/Name)[1]','int')
,#xml.value('(/*:orders/order/Header/Letter)[1]','varchar(max)');
Another alternative was this:
SELECT #xml.value('(//Name)[1]','int')
,#xml.value('(//Letter)[1]','varchar(max)');
But in general it is good advice, to be as specific as possible...

XML parsing error in SQL Server stored procedure

I want to parse the below XML in a SQL Server stored procedure and update some tables based on this XML. I have implemented the same using OPENXML but now there is one more line added to the beginning of the XML, because of which am getting unexpected errors. Is it possible to somehow skip the first tag alone while parsing
Parsing code :
set #Lead= (select lead
from openxml(#DOCHANDLE,'/DBO.TBLLEADS',2) with (lead INT 'LEAD'))`
XML here:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LEADS>
<LEAD>6680299</LEAD>
<JOBNO>50919</JOBNO>
<BEGINDATE>4-04-2013</BEGINDATE>
<ENDDATE>04/14/2013</ENDDATE>
</LEADS>
Well, not the most elegant solution, but will get it back to working:
Before you prepare your XML document, run this statement on the variable containing the XML:
SET #XMLVariable = REPLACE(#XMLVariable, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>', '')
Basically you're leveraging the REPLACE function to replace the unnecessary header string with nothing.

vb.net linq to xml syntax for documents with xml namespace

I'm trying to grasp the linq to xml 'inline query syntax' features of VB.Net
First I tried with this simple xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Child Name="somename">
<SomeAttribute>SomeValue</SomeAttribute>
</Child>
</Root>
This xml, when loaded in an XDocument, can be loaded and queried as follows:
Dim xdoc = XDocument.Load("sample.xml")
Console.WriteLine(xml.Root.<Child>.#Name)
Then I change the <Root> element in the sample xml file to:
<Root xmlns="http://SomeNamespace">
Now I can't seem to use the convenient 'Axis Properties' syntax anymore... I can only get it to work with the explicit XElement syntax:
Dim ns As XNamespace = "http://SomeNamespace"
' works, but I would like to use the same syntax as above...
Console.WriteLine(xdoc.Descendants(ns + "Child").First().Attribute("Name").Value)
I found the answer here
At first, I didn't know this syntactic feature was called "Axis Properties".
I had to add an Imports statement for the xml namespace:
Imports <xmlns:ns="http://SomeNamespace">
Then you can query with:
xdoc.Root.<ns:Child>.#Name

MSBuild XMLUpdate Question

I am using the XMLUpdate to update an xml formatted file in MSBuild. It updates fine but adds <?xml version="1.0" encoding="utf-8"?> at the top after update. Here is my statement that updates
<Import Project="C:\Program Files\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<XmlUpdate XmlFileName="$(AppName).alx" Xpath="/loader/application/version" Value="$(AppVersion)" />
Is it possible to update without the xml element at the top?
Thanks
Ponnu
The <?xml ...> is more of a descriptor than a real XML element. It describes your document and, for example defines the encoding. It won't interfere with your existing elements. I think it is even a standard feature of a XML document (but I don't have the specs handy)