Add additional nodes in XML - vb.net

I create an XML document using the below code
For Each cust As Customer In Customers
XDoc = <?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<Customers>
<Customer>
<Name>Mike</Name>
<Age>0</Age>
</Customer>
</Customers>
Next
XDoc.Save("C:\myXmlfile.xml")
However it only seems to add one record, but i dont know how to readd the nodes for each record? So if theres 2 records then i would expect
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<Customer>
<Name>Mike</Name>
<Age>0</Age>
</Customer>
<Customer>
<Name>Mike</Name>
<Age>0</Age>
</Customer>
but it should only generate one xml file.
Could anyone guide me in what i need to do please? Even after searching around im not 100% sure as theres too many methods and ive probably got confused.
Thanks

Inside the loop you set XDoc to the the customer. This means that when you save XDOC ONLY the last customer will be saved.
Even if you fix the above, you still have the issue that you are trying to add multiple root elements (each customer) to the XML document. This is not allowed, only one root element is allowed. So to produce a valid XML document, you want something like:
<Customers>
<Customer>
<Name>Mike</Name>
<Age>0</Age>
</Customer>
<Customer>
<Name>Mike</Name>
<Age>0</Age>
</Customer>
</Customers>
I'm sorry but I can recommend how to change your code, because I don't know the VB.Net extensions for XML. Hopefully, someone else will shed some light.
BTW, interesting handle.

You can generate XDocument and XElement separately.
Further, you can set data from variable using <%= %>.
Therefore, you should generate and combine like below.
' Generate customers
Dim customers As XEelement = <Customers></Customers>
For Each cust As Customer In Customers
customers.Add(
<Customer>
<Name><%= cust.Name %></Name>
<Age><%= cust.Age %></Age>
</Customer>
)
Next
' Combine customers
Dim XDoc As XDocument =
<?xml version="1.0" encoding="UTF-16" standalone="yes"?><%= customers %>
XDoc.Save("C:\myXmlfile.xml")

In addition to #RichardSchneider's answer, this is sample snippet in VB.
First, construct XDocument with single <Customers> element as root. Then in each iteration of For Each loop, add single <Customer> element to the root element of the XDocument :
XDoc As XDocument = <?xml version="1.0" encoding="UTF-16" standalone="yes"?><Customers></Customers>
For Each cust As Customer In customers
XDoc.Root.Add(
<Customer>
<Name>Mike</Name>
<Age>0</Age>
</Customer>
)
Next
XDoc.Save("C:\myXmlfile.xml")

Related

How to get id field from http xml response in power automate

How to get id field from http xml response in power automate
<?xml version="1.0" encoding="UTF-8"?>
<ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://test.qg.apps.test.com/qps/xsd/3.0/was/report.xsd">
<responseCode>SUCCESS</responseCode>
<count>1</count>
<data>
<Report>
<id>3505012</id>
</Report>
</data>
</ServiceResponse>
how can i get outcome 3505012 in result
tried below
#{slice(outputs('createreportxml'), add(nthIndexOf(outputs('createreportxml'), '>', 9), 1), nthIndexOf(outputs('createreportxml'), '<', 10))}
as well as below
xpath(xml(outputs('#{outputs('createreportxml')}'))), 'string(/ServiceResponse/data/Report/id/text())'
sample flow
Got it working with below has small issue with one braces.
#{xpath(xml(outputs('createreportxml')),'string(/ServiceResponse/data/Report/id/text())')}

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...

1st day on extracting the xml file from SQLServer, have Qs about 'PATH'

I'm reading a SQL fundamental textbook, and below is the T-SQL code:
SELECT Customer.custid AS [#custid],
Customer.companyname AS [companyname]
FROM Sales.Customers AS Customer
WHERE Customer.custid <= 2
ORDER BY Customer.custid
FOR XML PATH ('Customer'), ROOT('Customers');
Can anybody tell me the point of using PATH, and why there is an # before custid?
Any suggestions are welcomed, Thanks
The # marks the column as being an XML attribute instead of an element- ie: you'll get
<Customers>
<Customer custid="1234">
<companyname>Acme Sprockets Inc</companyname>
</Customer>
</Customers>
If you take the # away, you'd get
<Customers>
<Customer>
<custid>1234</custid>
<companyname>Acme Sprockets Inc</companyname>
</Customer>
</Customers>
Path allows you to customise the XML structures, without having to resort to FOR XML EXPLICIT
I'd start with the documentation online : http://technet.microsoft.com/en-us/library/ms189885.aspx

VB MS Word: Bind XML into an ordered lists?

Is there any way to bind ordered lists to XML documents in MS Word?
Currently I have xml such as:
<?xml version="1.0"?>
<myvars>
<name>foo</name>
</myvars>
Then I load and map it to a contentcontrol:
ActiveDocument.CustomXMLParts.Add
ActiveDocument.CustomXMLParts(5).Load ("c:\test.xml")
Dim strXPath1 As String
strXPath1 = "/myvars/name"
ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1
However this is only useful for single texts. Is there anyway to map xml enteries to ordered lists? For example:
<?xml version="1.0"?>
<myvars>
<person>
<name>foo</name>
<alias>bar</alias>
<alias>chew</alias>
</person>
<person>
<name>alpha</name>
<alias>bravo</alias>
<alias>charlie</alias>
</person>
</myvars>
mapped into the word document so that it shows up as ordered lists:
1. foo
a. bar
b. chew
2. alpha
a. bravo
b. charlie
Basically, the answer is you cannot. You can only map simple text and combo box type content controls to XML.

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