Click here to Skip to main content
15,868,072 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
Iam using vs2015. From C# I need to generate the XML file. Really I am very new to XML.
The tried the codes generates the XML file nicely.
C#
using namespace System::Xml;

    	XmlDocument^ Doc = gcnew XmlDocument();
		XmlDeclaration^ MyXmlDeclare = Doc->CreateXmlDeclaration("1.0", nullptr, nullptr);
		XmlElement^ MyRoot = Doc->DocumentElement;
		Doc->InsertBefore(MyXmlDeclare, MyRoot);

		XmlElement^ MyElement1 = Doc->CreateElement(String::Empty, "ENVELOPE", String::Empty);
		Doc->AppendChild(MyElement1);

		Doc->Save("C:\\Temp\\MyTallyPrgXML.xml");

But I get struck for the below XML lines. But I need it to generate it from C# 2015.
Kindly advice me to get succeed. Thanks
XML
<TALLYMESSAGE xmlns:UDF="TallyUDF">
  <VOUCHER VCHTYPE="Sales" ACTION="Create">
     <VOUCHERTYPENAME>Sales</VOUCHERTYPENAME>
     <ALLLEDGERENTRIES.LIST>
        <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES>
        <ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE>
        <LEDGERFROMITEM>No</LEDGERFROMITEM>
        <LEDGERNAME>Customer 1</LEDGERNAME>
        <AMOUNT>-5000</AMOUNT>
     </ALLLEDGERENTRIES.LIST>
  </VOUCHER>
</TALLYMESSAGE>


What I have tried:

I tried to generate the XML file from C# 2015.
Posted
Updated 12-Dec-21 22:58pm
v2
Comments
PIEBALDconsult 12-Dec-21 10:46am    
That doesn't look like C#.
Richard Deeming 13-Dec-21 4:52am    
Definitely not - it's Managed C++.

First, create your classes:
C#
[XmlRoot(ElementName="ALLLEDGERENTRIES.LIST")]
public class ALLLEDGERENTRIESLIST { 

	[XmlElement(ElementName="REMOVEZEROENTRIES")] 
	public string REMOVEZEROENTRIES { get; set; } 

	[XmlElement(ElementName="ISDEEMEDPOSITIVE")] 
	public string ISDEEMEDPOSITIVE { get; set; } 

	[XmlElement(ElementName="LEDGERFROMITEM")] 
	public string LEDGERFROMITEM { get; set; } 

	[XmlElement(ElementName="LEDGERNAME")] 
	public string LEDGERNAME { get; set; } 

	[XmlElement(ElementName="AMOUNT")] 
	public int AMOUNT { get; set; } 
}

[XmlRoot(ElementName="VOUCHER")]
public class VOUCHER { 

	[XmlElement(ElementName="VOUCHERTYPENAME")] 
	public string VOUCHERTYPENAME { get; set; } 

	[XmlElement(ElementName="ALLLEDGERENTRIES.LIST")] 
	public ALLLEDGERENTRIESLIST ALLLEDGERENTRIESLIST { get; set; } 

	[XmlAttribute(AttributeName="VCHTYPE")] 
	public string VCHTYPE { get; set; } 

	[XmlAttribute(AttributeName="ACTION")] 
	public string ACTION { get; set; } 

	[XmlText] 
	public string Text { get; set; } 
}

[XmlRoot(ElementName="TALLYMESSAGE")]
public class TALLYMESSAGE { 

	[XmlElement(ElementName="VOUCHER")] 
	public VOUCHER VOUCHER { get; set; } 

	[XmlAttribute(AttributeName="UDF")] 
	public string UDF { get; set; } 

	[XmlText] 
	public string Text { get; set; } 
}
Then populate them with data.

The code itself is trivial: How to write object data to an XML file (C#) | Microsoft Docs[^]
 
Share this answer
 
Comments
PIEBALDconsult 12-Dec-21 18:30pm    
Does that do the (unused) namespace?
OriginalGriff 13-Dec-21 2:09am    
It can do the hokey-cokey for all I care! :laugh:
Paramu1973 13-Dec-21 8:13am    
Thank OtiginalGriff, Can you teach me to convert it in Managed C++/CLR
Thanks Again
This is easy to do with LINQ to XML[^]:
C#
XDocument document = new XDocument(
    new XDeclaration("1.0", null, null),
    new XElement("TALLYMESSAGE",
        new XAttribute(XNamespace.Xmlns + "UDF", "TallyUDF"),
        new XElement("VOUCHER",
            new XAttribute("VCHTYPE", "Sales"),
            new XAttribute("ACTION", "Create"),
            new XElement("VOUCHERTYPENAME", "Sales"),
            new XElement("ALLLEDGERENTRIES.LIST",
                new XElement("REMOVEZEROENTRIES", "No"),
                new XElement("ISDEEMEDPOSITIVE", "Yes"),
                new XElement("LEDGERFROMITEM", "No"),
                new XElement("LEDGERNAME", "Customer 1"),
                new XElement("AMOUNT", -5000)
            )
        )
    )
);
 
Share this answer
 
Comments
Paramu1973 13-Dec-21 8:22am    
Thank Richard, can u explain me this line
XAttribute(XNamespace.Xmlns + "UDF", "TallyUDF")
Thanks
Richard Deeming 13-Dec-21 8:58am    
LINQ to XML uses XName instances to represent the name of elements and attributes.

If the name is in the default XML namespace, you can simply pass in a string, since there is an implicit conversion from string to XName.

If the name is in another namespace, you need to combine the local name with an instance of the XNamespace class.

In this case, your attribute is in the well-known xmlns namespace, so there is a static property on the XNamespace class to represent it.

Namespaces overview - LINQ to XML | Microsoft Docs[^]

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