Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign plain XML to C# variable [duplicate]

Tags:

c#

xml

linq

vb6

Possible Duplicate:
Use XML Literals in C#?

I can do this in Visual Basic, How can I do this in C#

    Dim xmlTree As XElement = <Employees></Employees>
like image 636
user357086 Avatar asked Jan 14 '13 04:01

user357086


People also ask

How to write an XML file in C?

Writing XML with C (or any other programming language) is really simple, you don't need any library (on the other hand reading and parsing XML is a bit more complex). As someone already noted, your did a mistake in your original XML expression. The content must be between 30 and 50000 characters.

What are the components of an XML file?

It consists of several modules: core syntax, meta-syntax, linking, style-bindings, and maybe more. Of these only the core syntax is common to all XML applications. Applications can choose to omit the other modules if they don't need them.

What should the XML syntax be?

Personal thoughts on what the XML syntax should be. Compare with my earlier notes. XML is a base-language for expressing arbitrary structured data in text form. It consists of several modules: core syntax, meta-syntax, linking, style-bindings, and maybe more. Of these only the core syntax is common to all XML applications.

How to parse an XML file?

You can either do this based on a specific structure and handcode the entire nested foreach loop mess, or if your text file format is consistent enough, you could probably write a recursive function to do it for you. The other method of parsing XML is to use an XmlReader.


2 Answers

Try:

XDocument document = XDocument.Parse("<Employees></Employees>")

or

XElement root = new XElement("Employees")

Another way is using XmlDocument class:

XmlDocument document = new XmlDocument();
document.LoadXml("<Employees></Employees>");

but I recommend to use XDocument. It is newer than XmlDocument, it has more clean API and support Linq To Xml.

like image 185
Alexander Balte Avatar answered Oct 13 '22 23:10

Alexander Balte


XDocument document = XDocument.Parse("<Employees></Employees>")
like image 39
user1331 Avatar answered Oct 13 '22 23:10

user1331