How to insert XML document to xml - c#

I have a XML document that I have to insert into data set..
Here's the part of the code:
string XmlFilePath = "C:\\Inetpub\\wwwroot\\Test_AGR_2.xml";
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(XmlFilePath);
So this XmlDoc I have to insert into a data-set but without any conversion to string or what so ever!.
Thanks in advance!

You can load XML directly to DataTable with DataTable.ReadXml function, then add DataTable to your DataSet.
https://msdn.microsoft.com/en-us/library/fs0z9zxd(v=vs.110).aspx

You can use DataTable.ReadXml function, BUT, the xml schema must be coherent to the dataset class.
The xml should look like this:
<?xml version="1.0" standalone="yes"?>
<testDS>
<testDT>
<testCol>0</testCol>
</testDT>
<testDT>
<testCol>1</testCol>
</testDT>
<testDT>
<testCol>2</testCol>
</testDT>
</testDS>
Instead you have to manually parse xml using XmlDocument and create the dataset with the data that you read.

Related

How do I edit Node Values in an Xml File with C#

I am trying to change the values in a Farming simulator 22 savegame xml file from C# in visual studio. There are a lot of nodes so I have reduced them to make things easier. I want to know how to replace the value in the node using C# with out having to create and rebuild the xml file from scratch.
the path to the xml file is: (C:\Users\Name\Documents\My Games\FarmingSimulator2022\savegame1\careerSavegame.xml)
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<careerSavegame revision="2" valid="true">
<settings>
<savegameName>My game save</savegameName>
<creationDate>2022-05-03</creationDate>
<mapId>MapFR</mapId>
<mapTitle>Haut-Beyleron</mapTitle>
<saveDateFormatted>2022-08-22</saveDateFormatted>
<saveDate>2022-08-22</saveDate>
<resetVehicles>false</resetVehicles>
</careerSavegame>
You can use the System.Xml.Linq namespace to access the xml file. This will load the file in the memory.
There is one class inside it, XDocument, that represents the xml document.
String filePath = "C:\Users\Name\Documents\My Games\FarmingSimulator2022\savegame1\careerSavegame.xml"
XDocument xdoc = XDocument.Load(filePath);
var element = xdoc.Elements("MyXmlElement").Single();
element.Value = "foo";
xdoc.Save("file.xml");
You can set the element variable as per the one which is needed to be replaced.
Through some research I found the solution to editing the values within the nodes. In this example I only change the value of savegameName, but it will be the same for the rest.
//Routing the xml file
XmlDocument xmlsettings = new XmlDocument();
xmlsettings.Load(#"D:\careerSavegame.xml");
//Setting values to nodes through innertext
String FarmNameSetting = "Martek Farm";
XmlNode savegameNamenode =
xmlsettings.SelectSingleNode
("careerSavegame/settings/savegameName");
savegameNamenode.InnerText = FarmNameSetting;

Cannot convert XElement to XObject

I am trying to serialize an XML file using the following code:
XmlDocument xDoc = new XmlDocument();
xDoc.Load(#"D:\myfile.xml");
string jsonStr = JsonConvert.SerializeXNode(xDoc);
but it's not working and I am getting following error on 3rd line
Cannot convert XmlDocument into XObject
I also tried to find the first node and then try to pass it but its also not working.
You're using XmlDocument, which is from the "old" XML API. Json.NET uses the "new" XML API of LINQ to XML. You just need to change how you're loading the XML:
XDocument xml = XDocument.Load(#"D:\myfile.xml");
string json = JsonConvert.SerializeXNode(xml);

Unable to load XmlReader into an XmlDocument

<Response>
<Items>
<attributes></attributes>
</Items>
<Locations>
<attributes></attributes>
</Locations>
</Response>
I have an XML file in the format shown above.
I want to save attributes of parent Items in a separate table in the database and attributes of parent Locations in another table.
I am not able to use XmlReader element .Read(). I have been using XmlDocument.Load to initially load the reader as XmlDocument and by using
doc.SelectSingleNode("//Items") will be selecting nodes and will parse through the inside information through XmlReader .
But now I am facing an issue in loading XmlReader into an XmlDocument as I get an OutOfMemoryException.
Can someone help me to solve this?

load xml file into memory

I need to load a xml file in memory so I can access it several times from different forms.
The xml is in this format:
<Slides>
<Slide>
<Name>Name 1</Name>
<Value>Value 1</Value>
<Type>Type 1</Type>
</Slide>
<Slide>
<Name>Name 2</Name>
<Value>Value 2</Value>
<Type>Type 2</Type>
</Slide>
</Slides>
I don't want to use a database to store the vars. Is there any other method to store the data in memory?
Right now I'm using a txt file for each slide and streamreader, which I'm sure is not the best option.
EDIT:
I added this code, but will I be able to get the slides every time without reading the xml file again?
var slides = from s in XElement.Load("slides.xml").Elements("Slide")
select s;
foreach (var slide in slides)
{
//code
}
You should use the XDocument Load method: http://msdn.microsoft.com/en-us/library/bb343181.aspx
i think it is better to use XDocument than the XmlDocument... but it depends on your requirements...
The XDocument is more memory optimized than the XmlDocument, and i think they both are easy for use (for getting values from the xml)
See the XmlDocument class. The Load() method does what you want.
First you have to save the xml file in your project file or your hard disk.Then only you can load the xml file in code behind.
You can create xml file dynamically.it will be much better.
using System.Xml;
XmlDocument myxml = new XmlDocument();
myxml.Load("D:/sample.xml");//Load you xml file from you disk what you want to load
string element_1 = myxml.GetElementsByTagName("Name")[0].InnerText;
string element_2 = myxml.GetElementsByTagName("Value")[0].InnerText;
string element_3 = myxml.GetElementsByTagName("Value")[0].InnerText;
string element_4 = myxml.GetElementsByTagName("Name")[1].InnerText;
string element_5 = myxml.GetElementsByTagName("Value")[1].InnerText;
string element_6 = myxml.GetElementsByTagName("Value")[1].InnerText;
Try this program it will help you.

How to Read XML into a DataSet

I have a class that goes to a URL and gets a xml document using xmlDoc.Load(URL). To test the class, I added a web project to display the xml in a grid view.
In a button click I create an instance of an xml document and populate it as:
xmlDoc = myClassName()
I'm stuck at how to get xmlDoc into a format usable by the datasource
I am totally confused as to how to get the xml to be displayed in the grid as dataset.ReadXml seems to want a file path. I don't understand the other overloads. I suppose I have to read the xml into a string or something else, but I don't understand how to do this - even after reading numerous posts here and MSDN - Thanks!
Example:
string xml =#"<xml><customer><id>1</id></customer></xml>";
DataSet ds = new DataSet();
ds.ReadXml(XmlReader.Create(new StringReader(xml)));
Now set the datasource to your grid:
grid.DataSource=newDataSet.Tables[0];
Update:
DataSet ds = new DataSet();
//xmlDocument is your XmlDocument instance
ds.ReadXml(XmlReader.Create(new StringReader(xmlDocument.InnerXml)));
grid.DataSource=newDataSet.Tables[0];

Categories

Resources