I want to create a XML file in csharp like the below format pls help me with the code
<MasterEntries>
<fruit>Apple</fruit>
<animal>Fox</animal>
<color>Violet</color>
</MasterEntries>
Well, if you have .NET 3.5 available to you, I'd recommend you use LINQ to XML. For example:
XElement master = new XElement("MasterEntries",
new XElement("fruit", "Apple"),
new XElement("animal", "Fox"),
new XElement("color", "Violet"));
That's about as simple as it gets :)
EDIT: Okay, in .NET 2.0 it's a bit more cumbersome. Something like this:
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("MasterEntries");
doc.AppendChild(root);
XmlElement fruit = doc.CreateElement("fruit");
fruit.InnerText = "Apple";
root.AppendChild(fruit);
XmlElement animal = doc.CreateElement("animal");
animal.InnerText = "Fox";
root.AppendChild(animal);
XmlElement color = doc.CreateElement("color");
color.InnerText = "Violet";
root.AppendChild(color);
There may well be simpler ways of doing this, but I don't know them...
Once you've got an XElement/XDocument/XmlDocument, you can call Save to save it to a file.
Related
I'm currently working on a small weather application in C#. To do this, I need to extract data from this xml file: http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22london%22&format=xml
In this specific case I need the value of the first /query/results/place/woeid node. I've been looking around and tried many different methods, but didn't manage to get any values with any of those. My current code looks like this:
string query = String.Format("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22london%22&format=xml");
XmlDocument xml = new XmlDocument();
xml.Load(query);
XmlNodeList nodeList = wData.DocumentElement.SelectNodes("/query/results/place");
foreach (XmlNode node in nodeList)
{
return node.SelectSingleNode("woeid").InnerText;
}
return "NO WOEID FOUND!";
I'm just starting to learn C# so I might do some stupid mistakes. Still, I would really appreciate any kind of help.
You can use XDocument
string query = String.Format("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22london%22&format=xml");
XDocument xml = XDocument.Load(query);
XNamespace ns = "http://where.yahooapis.com/v1/schema.rng";
var woeid = xml.Element("query").Element("results").Elements(ns + "place").FirstOrDefault().Element(ns +"woeid").Value;
Make sure you check if there are any elements in the XDocuments
I have to import XML-documents into a SQL-DB via C#. The XML has several namespaces.
I tried to address the specific elements I want wo import via XPath, but it broke due to the namespaces. Instead of
XmlDocument doc = new XmlDocument();
doc = XDocument.Load(Filename);
GENERATOR_INFO = doc.SelectSingleNode("ORDER/ORDER_HEADER/ORDER_INFO/GENERATOR_INFO").InnerText;
I write something like this:
XmlDocument doc = new XmlDocument();
doc = XDocument.Load(Filename);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("openTrans", "http://www.opentrans.org/XMLSchema/2.1");
GENERATOR_INFO = doc.SelectSingleNode("//openTrans:GENERATOR_INFO", nsmgr).InnerText;
thus shortening the "path" to the element I need the Value of.
My question is: How can I address an element directly? In the XMl there are two elements called "ORDER_ID":
ORDER/ORDER_HEADER/ORDER_INFO/ORDER_ID
and
ORDER/ORDER_HEADER/CUSTOMER_ORDER_REFERENCE/ORDER_INFO
When using the syntax above I get only one element, with a loop/Linq I get both values but don't know, which one is which.
Is there an option to address the elements by there unique XPath despite the namespace?
regards
Jens
Found it :)
I have to reference every "step" of the XPath: ORDER_ID = doc.SelectSingleNode("//openTrans:ORDER_INFO/openTrans:ORDER_ID", nsmgr).InnerText; resp. VIND_ORDER_ID = COR.SelectSingleNode("//openTrans:CUSTOMER_ORDER_REFERENCE/openTrans:ORDER_ID", nsmgr).InnerText
if i were to say create a function:
public static string createProduct(string pName, decimal pPrice)
{string postData = #"<?xml version=""1.0"" encoding=""UTF-8""?
<product>
<name>?</name>
<price>?</price>
</product>";
....some other codes...}
i have tried googling it but have not found any clear answer...
and sorry i'm kinda new to xml and c#, so how do i insert pName and pPrice into xml without upsetting visual studio?
would really appreciate help from you guys...tia!
var str = new XElement("product",
new XElement("name", pName),
new XElement("price", pPrice))
.ToString();
System.Xml.Linq Namespace and XElement class are good places to start to read and easier to use than System.Xml namespace
public static string createProduct(string pName, decimal pPrice)
{string postData = #"<?xml version=""1.0"" encoding=""UTF-8""?
<product>
<name>" + pName + #"</name>
<price>" + pPrice+ #"</price>
</product>";
....some other codes...}
I recommend that you take a look at constructing the XML document not by string concatenation as it is quite unreliable (what if pName contains angle brackets?). Try looking at the XElement (XLINQ) API.
A Google search for how to add variables to a string in c# yielded this page as the first result: Strings (C# Programming Guide)
You can use the + operator to concatenate strings (that compiles to the string.Concat method). Or, you could use string.Format; for more information on that, see the page on composite formatting. This approach is only suitable for the simplest of small XML fragments.
You also have several options in the form of different sets of classes that will help you build and work with XML, most of which are to be found in the System.Xml namespaces. These classes will produce well-formed XML, taking care of small details that you might overlook (for example, special handling of certain characters). Unfortunately, I don't know of a good discussion of the pros and cons of each approach.
I'd really recommend not doing this by string concatenation. There are a number of different ways to accomplish this that will yield better (as in less likely to produce malformed XML) results.
Via XmlTextWriter:
string xmlString = null;
using (StringWriter xmlOutput = new StringWriter())
using(XmlTextWriter xmlWriter = new XmlTextWriter(xmlOutput))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("product");
xmlWriter.WriteElementString("name", pName);
xmlWriter.WriteElementString("price", pPrice);
xmlWriter.WriteEndElement();
xmlString = xmlOutput.ToString();
}
Using XmlDocument:
string xmlString = null;
using (StringWriter xmlOutput = new StringWriter())
{
XmlDocument xmlDocument = new XmlDocument();
XmlElement productElement = xmlDocument.CreateElement("product");
XmlElement nameElement = xmlDocument.CreateElement("name");
nameElement.InnerText = pName;
XmlElement priceElement = xmlDocument.CreateElement("price");
priceElement.InnerText = pPrice;
productElement.AppendChild(nameElement);
productElement.AppendChild(priceElement);
xmlDocument.AppendChild(productElement);
xmlDocument.Save(xmlOutput);
xmlString = xmlOutput.ToString();
}
Using XDocument (requires that you are using .NET 3.5 or higher):
XDocument xml = new XDocument(
new XElement("product",
new XElement("name", pName),
new XElement("price", pPrice)
)
);
string xmlString = xml.ToString();
Note that of these methods only the one using XmlTextWriter will stream, which may be important for very large XML composition. If you are using .NET 3.5 or higher and are not dealing with very large XML composition, I would give preference to XDocument as it's a lot more readable and simpler to use.
I have XML stored in string variable:
<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>
Here I want to change XML tag <ItemMasterList> to <Masterlist>. How can I do this?
System.Xml.XmlDocument and the associated classes in that same namespace will prove invaluable to you here.
XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("MasterList");
docNew.AppendChild(newRoot);
newRoot.InnerXml = doc.DocumentElement.InnerXml;
String xml = docNew.OuterXml;
I know i am a bit late, but just have to add this answer as no one seems to know about this.
XDocument doc = XDocument.Parse("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
doc.Root.Name = "MasterList";
Which returns the following:
<MasterList>
<ItemMaster>
<fpartno>xxx</fpartno>
<frev>000</frev>
<fac>Default</fac>
</ItemMaster>
</MasterList>
You can use LINQ to XML to parse the XML string, create a new root and add the child elements and attributes of the original root to the new root:
XDocument doc = XDocument.Parse("<ItemMasterList>...</ItemMasterList>");
XDocument result = new XDocument(
new XElement("Masterlist", doc.Root.Attributes(), doc.Root.Nodes()));
Using the XmlDocument way, you can do this as follows (and keep the tree intact):
XmlDocument oldDoc = new XmlDocument();
oldDoc.LoadXml("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
XmlNode node = oldDoc.SelectSingleNode("ItemMasterList");
XmlDocument newDoc = new XmlDocument();
XmlElement ele = newDoc.CreateElement("MasterList");
ele.InnerXml = node.InnerXml;
If you now use ele.OuterXml is will return: (you you just need the string, otherwise use XmlDocument.AppendChild(ele) and you will be able to use the XmlDocument object some more)
<MasterList>
<ItemMaster>
<fpartno>xxx</fpartno>
<frev>000</frev>
<fac>Default</fac>
</ItemMaster>
</MasterList>
As pointed by Will A, we can do it that way but for case where InnerXml equals the OuterXml the following solution will work out:
// Create a new Xml doc object with root node as "NewRootNode" and
// copy the inner content from old doc object using the LastChild.
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("NewRootNode");
docNew.AppendChild(newRoot);
// The below line solves the InnerXml equals the OuterXml Problem
newRoot.InnerXml = oldDoc.LastChild.InnerXml;
string xmlText = docNew.OuterXml;
I want to save and load my xml data using XmlReader. But I don't know how to use this class. Can you give me a sample code for start?
MSDN has a simple example to get you started here.
If you're interested in reading and writing XML documents, and not just specifically using the XmlReader class, there's a nice article covering a few of your options here.
But if you just want to get started and play around, try this:
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create("file.xml", settings);
Personally I have switched away from XMLReader to System.XML.Linq.XDocument to manage my XML data files. This way I can easily pull data from xml into objects and manage them like any other object in my program. When I am done manipulating them I can just save the changes back out the the xml file at any time.
//Load my xml document
XDocument myData = XDocument.Load(PhysicalApplicationPath + "/Data.xml");
//Create my new object
HelpItem newitem = new HelpItem();
newitem.Answer = answer;
newitem.Question = question;
newitem.Category = category;
//Find the Parent Node and then add the new item to it.
XElement helpItems = myData.Descendants("HelpItems").First();
helpItems.Add(newitem.XmlHelpItem());
//then save it back out to the file system
myData.Save(PhysicalApplicationPath + "/Data.xml");
If I want to use this data in an easily managed data set I can bind it to a list of my objects.
List<HelpItem> helpitems = (from helpitem in myData.Descendants("HelpItem")
select new HelpItem
{
Category = helpitem.Element("Category").Value,
Question = helpitem.Element("Question").Value,
Answer = helpitem.Element("Answer").Value,
}).ToList<HelpItem>();
Now it can be passed around and manipulated with any inherent functions of my object class.
For convenience my class has a function to create itself as an xml node.
public XElement XmlHelpItem()
{
XElement helpitem = new XElement("HelpItem");
XElement category = new XElement("Category", Category);
XElement question = new XElement("Question", Question);
XElement answer = new XElement("Answer", Answer);
helpitem.Add(category);
helpitem.Add(question);
helpitem.Add(answer);
return helpitem;
}
You should use the Create method instead of using new, since XmlReader is an abstract class using the Factory pattern.
var xmlReader = XmlReader.Create("xmlfile.xml");
From the excellent C# 3.0 in a Nutshell, consider looking at the sample code from chapter 11.