I am trying to construct a .xml file of the form
<Orders>
<Id type="System.Int32">1</Id>
<OrderItems>
<OrderItem>
<Id type="System.Int32">321</Id>
<Product type="System.String">Coffee</Product>
</OrderItem>
</OrderItems>
<Client type="System.String">Johnny</Client>
<Orders>
For Order model:
public class Order
{
public int Id { get; set; }
public List<OrderItem> Products { get; set; }
public string Client { get; set; }
}
Here, I create the Order element
public void SaveToFile(IEnumerable<Order> elementsList)
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
xmlDoc.PrependChild(xmlDec);
XmlElement elemRoot = xmlDoc.CreateElement("Orders");
xmlDoc.AppendChild(elemRoot);
XmlHelper<Order> xmlHelper = new XmlHelper<Order>();
foreach (var order in _orders)
{
xmlHelper.AddNodeToXmlDocument(xmlDoc, elemRoot, order);
}
xmlDoc.PreserveWhitespace = true;
xmlDoc.Save(_filePath);
}
And here, I am trying to construct the sub-elements. It works fine for Id and Client, but when I try to create the order items, I get this error at line document.AppendChild(elemRoot);
public void AddNodeToXmlDocument(XmlDocument document, XmlElement rootElement, object myObject)
{
XmlElement myObjectElement = document.CreateElement(EntityFormatter.GetObjectName(myObject));
foreach (var objectProperty in EntityFormatter.GetPropertiesAndValues(myObject))
{
if ((objectProperty.Value.GetType().FullName).ToString().Contains("System.Collections.Generic.List"))
{
Regex regex = new Regex(#"Models[.][A-Za-z]+");
Match match = regex.Match(objectProperty.Value.ToString());
var elemRoot = document.CreateElement(match.Value.Substring(7));
document.AppendChild(elemRoot);
foreach (var obj in objectProperty.Value.ToString())
{
AddNodeToXmlDocument(document, elemRoot, obj);
}
}
else
{
var elem = document.CreateElement(objectProperty.Key);
elem.SetAttribute("type", objectProperty.Value.GetType().FullName);
elem.InnerText = objectProperty.Value.ToString();
myObjectElement.AppendChild(elem);
}
}
rootElement.AppendChild(myObjectElement);
}
XML specification only allows single root element in a document. document.AppendChild(elemRoot) line in your AddNodeToXmlDocument() method throws exception because root element has been created before in the SaveToFile() method :
.....
XmlElement elemRoot = xmlDoc.CreateElement("Orders");
xmlDoc.AppendChild(elemRoot);
.....
It isn't clear what you're trying to do with the erroneous line, maybe you want to append elemRoot to the previously created root element instead :
.....
var elemRoot = document.CreateElement(match.Value.Substring(7));
document.DocumentElement.AppendChild(elemRoot);
.....
Related
It's my fist time working with XML and I use a basic XML file format like this:
<root>
<staff id="1">
<name>name 1</name>
<phone>123456</phone>
</staff>
<staff id="2">
<name>name 2</name>
<phone>123789</phone>
<phone2>123789</phone2>
</staff>
</root>
some nodes have more elements (phone2 in this case). I want to add (or remove) an element on a node. I'm creating a WinForms in C# that work with this XML. I'm doing:
I read the XML to have a XmlNodeList variable.
From XmlNodeList I get the node that I want modify to a XmlNode variable.
I modify name or phone on XmlNode
I read again the XML file and I update the correct node with the XmlNode variable new info.
My problem is I don't know how add (or remove) the element "phone2" on my XmlNode variable.
program.cs:
public static XmlNode staff;
public static XmlNodeList xnList = GetList();
public static XmlNodeList GetList()
{
XmlNodeList xnList;
XmlDocument doc = new XmlDocument();
doc.Load(path);
xnList = doc.SelectNodes("/root/staff");
return xnList;
}
public static void GetID(string id)
{
foreach (XmlNode xn in xnList)
{
if(xn.Attributes["id"].Value == id)
{
staff = xn;
}
}
}
form1.cs
private void btnOK_Click(object sender, EventArgs e)
{
Program.staff["name"].InnerText = textBoxName.Text;
Program.staff["phone"].InnerText = textBoxPhone.Text;
if (Program.staff.SelectSingleNode("phone2") == null)
{
// here I want to create "phone2" in Program.staff if not exist
// to update XML file later.
Program.staff["phone2"].InnerText = textBoxPhone2.Text;
}
}
I don't find the correct method to do it and maybe it's not the best way to do it, but I accept suggestions...
There are multiple ways to work with XML files. I'll show two options below.
Test.xml:
<root>
<staff id="1">
<name>Name 1</name>
<phone>123456</phone>
</staff>
<staff id="2">
<name>Name 2</name>
<phone>123457</phone>
<phone>123458</phone>
</staff>
</root>
Option 1 (LINQ to XML):
Add the following using directive:
using System.Xml.Linq;
CreateXmlLinq:
private void CreateXmlLinq(string filename)
{
XElement root = new XElement("root",
new XElement("staff", new XAttribute("id", "1"),
new XElement("name", "Name 1"),
new XElement("phone", "123456")),
new XElement("staff", new XAttribute("id", "2"),
new XElement("name", "Name 2"),
new XElement("phone", "123457"),
new XElement("phone", "123458"))
);
root.Save(filename);
}
Usage:
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "XML File (*.xml)|*.xml";
sfd.FileName = "Test.xml";
if (sfd.ShowDialog() == DialogResult.OK)
{
//save to file
CreateXmlLinq(sfd.FileName);
Debug.WriteLine($"Info: Saved to {sfd.FileName}");
}
}
To remove phone number 123458 where staff id = 2:
RemovePhone:
private void RemovePhone(string filename, string id, string phoneNumber)
{
//load from file
XElement root = XElement.Load(filename);
//remove specified phone number
root.Elements("staff").Where(s => s.Attribute("id").Value == id).Elements("phone").Where(p => p.Value == phoneNumber).Remove();
//save to file
root.Save(filename);
}
Option 2 (XML Serialization):
For this approach, we'll use nested classes.
Add the following using directives to each of the classes:
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
You can name the classes whatever you like, I've chosen to prepend the word "Xml". Additionally for the nested classes, I've chosen to append the ancestors' names. In this case, there is only one ancestor (the parent) "root".
XmlRoot
XmlRootStaff: ("XmlRoot" + "Staff)
XmlRoot.cs:
[XmlRoot(ElementName = "root", IsNullable = false)]
public class XmlRoot
{
[XmlElement(ElementName = "staff")]
public List<XmlRootStaff> Staff { get; set; } = new List<XmlRootStaff>();
}
XmlRootStaff.cs:
public class XmlRootStaff
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "phone")]
public List<string> Phone { get; set; } = new List<string>();
}
To deserialize the XML (read from file) we'll use the following method:
DeserializeXMLFileToObject:
public static T DeserializeXMLFileToObject<T>(string xmlFilename)
{
T rObject = default(T);
try
{
if (string.IsNullOrEmpty(xmlFilename))
{
return default(T);
}
using (System.IO.StreamReader xmlStream = new System.IO.StreamReader(xmlFilename))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
rObject = (T)serializer.Deserialize(xmlStream);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error (DeserializeXMLFileToObject) - {ex.Message}");
throw;
}
return rObject;
}
Usage (deserialize):
private XmlRoot _root = null;
...
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "XML File (*.xml)|*.xml";
ofd.FileName = "Test.xml";
if (ofd.ShowDialog() == DialogResult.OK)
{
//deserialize
_root = HelperXml.DeserializeXMLFileToObject<XmlRoot>(ofd.FileName);
}
}
To serialize the XML (write to file) we'll use the following method:
SerializeObjectToXMLFile:
public static void SerializeObjectToXMLFile(object obj, string xmlFilename)
{
try
{
if (string.IsNullOrEmpty(xmlFilename))
{
return;
}//if
System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
settings.OmitXmlDeclaration = false;
settings.Indent = true;
settings.NewLineHandling = System.Xml.NewLineHandling.Entitize;
using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(xmlFilename, settings))
{
//specify namespaces
System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
ns.Add(string.Empty, "urn:none");
//create new instance
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
//write XML to file
serializer.Serialize(xmlWriter, obj, ns);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error (SerializeObjectToXMLFile) - {ex.Message}");
throw;
}
}
Usage (serialize):
private XmlRoot _root = null;
...
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "XML File (*.xml)|*.xml";
sfd.FileName = "Test.xml";
if (sfd.ShowDialog() == DialogResult.OK)
{
//create new instance
_root = new XmlRoot();
//add data
_root.Staff.Add(new XmlRootStaff() { Id = "1", Name = "Name 1", Phone = new List<string>() { "123456" } });
_root.Staff.Add(new XmlRootStaff() { Id = "2", Name = "Name 2", Phone = new List<string>() { "123457", "123458" } });
//serialize - save to file
SerializeObjectToXMLFile(_root, sfd.FileName);
Debug.WriteLine($"Info: Saved to {sfd.FileName}");
}
}
To remove phone number 123458 where staff id = 2:
private void RemovePhone(string id, string phoneNumber)
{
if (_root != null)
{
for (int i = 0; i < _root.Staff.Count; i++)
{
if (_root.Staff[i].Id == id)
{
//remove
_root.Staff[i].Phone.Remove(phoneNumber);
break;
}
}
}
}
Resources:
LINQ to XML overview
XElement.Save Method
How to delete specific nodes from an XElement?
XML serialization
Examples of XML Serialization
Finally I solved changing:
program.cs
public static XmlDocument doc = new XmlDocument(); // ADDED THIS HERE
public static XmlNode staff;
public static XmlNodeList xnList = GetList();
public static XmlNodeList GetList()
{
XmlNodeList xnList;
// REMOVED XmlDocument from HERE
doc.Load(path);
xnList = doc.SelectNodes("/root/staff");
return xnList;
}
public static void GetID(string id)
{
foreach (XmlNode xn in xnList)
{
if(xn.Attributes["id"].Value == id)
{
staff = xn;
}
}
}
form1.cs
private void btnOK_Click(object sender, EventArgs e)
{
Program.staff["name"].InnerText = textBoxName.Text;
Program.staff["phone"].InnerText = textBoxPhone.Text;
if (Program.staff.SelectSingleNode("phone2") == null)
{
XmlElement elem = Program.doc.CreateElement("phone2");
elem.InnerText = textBoxPhone2.Text;
Program.staff.AppendChild(elem);
}
}
Is there a way of parsing certain data from an XML file, and outputting that information onto an excel(csv) file?
Use this code. You need to convert xmldocument to xdocument. So you can easily capture each element and its data. I used the same file you provided. I also mentioned example of how to read elements in loop and its child.
class Program
{
static void Main(string[] args)
{
Parse();
}
public static void Parse()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(#"D:\New Text Document.xsd");
var captureElements = new List<CustomElements>();
var xdocument = xmlDoc.ToXDocument();
foreach (var element in xdocument.Elements())
{
foreach (var node in element.Elements()) //childs...
{
if (node.Name.LocalName.Equals("ElementType"))
{
foreach (var scopeNode in node.Elements())
{
if (scopeNode.Name.LocalName.Equals("element"))
{
var xml = XElement.Parse(scopeNode.ToString());
var customElement = new CustomElements();
customElement.Type = xml.Attribute("type")?.Value;
customElement.Label = xml.Attribute("label")?.Value;
customElement.CompTypes = xml.Attribute("CompTypes")?.Value;
customElement.Readonly = xml.Attribute("readonly")?.Value;
customElement.Hidden = xml.Attribute("hidden")?.Value;
customElement.Require = xml.Attribute("require")?.Value;
captureElements.Add(customElement);
}
}
}
}
}
}
}
public static class DocumentExtensions
{
public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
var xmlDocument = new XmlDocument();
using (var xmlReader = xDocument.CreateReader())
{
xmlDocument.Load(xmlReader);
}
return xmlDocument;
}
public static XDocument ToXDocument(this XmlDocument xmlDocument)
{
using (var nodeReader = new XmlNodeReader(xmlDocument))
{
nodeReader.MoveToContent();
return XDocument.Load(nodeReader);
}
}
}
public class CustomElements
{
public string Type { get; set; }
public string Label { get; set; }
public string CompTypes { get; set; }
public string Readonly { get; set; }
public string Hidden { get; set; }
public string Require { get; set; }
}
Very easily done in XSLT. You don't need the schema. Unless there are special characters that need to be escaped, etc, it's as simple as:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" expand-text="yes">
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="text"/>
<xsl:template match="Element"
>{#type},{#label},{#CompTypes},{#readonly},{#hidden},{#required}
</xsl:template>
</xsl:transform>
That's an XSLT 3.0 solution; if you prefer to use the XSLT 1.0 processor that comes bundled with .NET that's a bit more verbose but still quite straightforward. I haven't included a header line but it's a simple matter to add it.
In XSLT 3.0 you can even add the automation to apply this to a whole collection of XML files (in 1.0 you would need to do that in a calling script).
In C#, use System.Xml.XmlDocument, and XPath syntax in SelectNodes
XmlDocument xml = new XmlDocument();
xml.Load( strFile );
foreach (XmlElement ndRow in xml.SelectNodes("//element")) {
string strType = ndRow.GetAttribute("type");
string strLabel = ndRow.GetAttribute("label");
}
I have a List of Class object.
List<SDKReq> SDKReqList = new List<SDKReq>()
The class SDKReq has members as class again
public class SDKReq
{
public List<String> Identifier { get; set; }
public IDictionary<String, String> Prop { get; set; }
}
I need this List object to converted to XML and saved to text file. Please note that this class is not serializable.
You can do something like this:
public string CreateXml(List<SDKReq> list)
{
//first create the xml declaration
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
//second create a container node for all SDKReq objects
XmlNode SdkListNode = doc.CreateElement("SDKReqList");
doc.AppendChild(SdkListNode);
//iterate through all SDKReq objects and create a container node for each of it
foreach (SDKReq item in list)
{
XmlNode sdkNode = doc.CreateElement("SDKReq");
SdkListNode.AppendChild(sdkNode);
//create a container node for all strings in SDKReq.Identifier
XmlNode IdListNode = doc.CreateElement("Identifiers");
sdkNode.AppendChild(IdListNode);
//iterate through all SDKReq.Identifiers and create a node foreach of it
foreach (string s in item.Identifier)
{
XmlNode idNode = doc.CreateElement("Identifier");
idNode.InnerText = s;
IdListNode.AppendChild(idNode);
}
//create a container node for all SDKReq.Prop
XmlNode propListNode = doc.CreateElement("Props");
sdkNode.AppendChild(propListNode);
//iterate through all SDKReq.Prop and create a node for each of it
foreach (KeyValuePair<string, string> kvp in item.Prop)
{
//since the SDKReq.Prop is a dictionary, you should add both, key and value, to the node.
//i decided to add an attribute 'key' for the key
XmlNode propNode = doc.CreateElement("Prop");
XmlAttribute attribute = doc.CreateAttribute("key");
attribute.Value = kvp.Key;
propNode.Attributes.Append(attribute);
propNode.InnerText = kvp.Value;
propListNode.AppendChild(propNode);
}
}
return doc.InnerXml;
}
I havent tested it, but the output should look like this:
<?xml version="1.0" encoding="utf-8"?>
<SDKReqList>
<SDKReq>
<Identifiers>
<Identifier>1234</Identifier>
<Identifier>5678</Identifier>
</Identifiers>
<Props>
<Prop key="abc11">Value</Prop>
<Prop key="abc12">Value</Prop>
</Props>
</SDKReq>
<SDKReq>
<Identifiers>
<Identifier>8765</Identifier>
<Identifier>4321</Identifier>
</Identifiers>
<Props>
<Prop key="def22">Value</Prop>
<Prop key="def23">Value</Prop>
</Props>
</SDKReq>
</SDKReqList>
I am loading my data from XML using C# this way:
XmlDocument xmlDoc = new XmlDocument();
TextAsset xmlFile = Resources.Load("levels/" + levelID) as TextAsset;
xmlDoc.LoadXml(xmlFile.text);
XmlNodeList levelsList = xmlDoc.GetElementsByTagName("level");
foreach (XmlNode levelInfo in levelsList)
{
XmlNodeList childNodes = levelInfo.ChildNodes;
foreach (XmlNode value in childNodes)
{
switch (value.Name)
{
case "info":
//levelWidth = getInt(value, 0);
//levelHeight = getInt(value, 1);
break;
}
}
}
And heres XML I am loading:
<?xml version="1.0" encoding="utf-8" ?>
<level>
<info w="1000" h="500"/>
</level>
It works just fine, I am now trying to find best way to load child nodes, inside my level node with multiple points nodes inside
<?xml version="1.0" encoding="utf-8" ?>
<level>
<info w="1000" h="500"/>
<ground>
<point val1="val1" val2="val2"/>
</ground>
</level>
I will be grateful for some guidance how to move in the right direction, thank you.
Using XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<level>" +
"<info w=\"1000\" h=\"500\"/>" +
"</level>";
XDocument doc = XDocument.Parse(xml);
XElement level = (XElement)doc.FirstNode;
level.Add("ground", new object[] {
new XElement("point", new XAttribute[] {
new XAttribute("val1", "val1"),
new XAttribute("val2", "val2")
})
});
}
}
}
If you need read all points, you can use
var nodeList = Xmldocument.SelectNodes("level/info/ground/point");
SelectNodes return a list of nodes.
I would go for a slidely different way and use a data object. Then you don't have to analyse xml, you just code your data class:
[Serializable()]
public class CLevel
{
public string Info { get; set; }
}
[Serializable()]
public class CDatafile
{
public List<CLevel> LevelList { get; set; }
public CDatafile()
{
LevelList = new List<CLevel>();
}
}
public class DataManager
{
private string FileName = "Data.xml";
public CDatafile Datafile { get; set; }
public DataManager()
{
Datafile = new CDatafile();
}
// Load file
public void LoadFile()
{
if (System.IO.File.Exists(FileName))
{
System.IO.StreamReader srReader = System.IO.File.OpenText(FileName);
Type tType = Datafile.GetType();
System.Xml.Serialization.XmlSerializer xsSerializer = new System.Xml.Serialization.XmlSerializer(tType);
object oData = xsSerializer.Deserialize(srReader);
Datafile = (CDatafile)oData;
srReader.Close();
}
}
// Save file
public void SaveFile()
{
System.IO.StreamWriter swWriter = System.IO.File.CreateText(FileName);
Type tType = Datafile.GetType();
if (tType.IsSerializable)
{
System.Xml.Serialization.XmlSerializer xsSerializer = new System.Xml.Serialization.XmlSerializer(tType);
xsSerializer.Serialize(swWriter, Datafile);
swWriter.Close();
}
}
Then you can use it to create, save and load the file like this:
DataManager dataMng = new DataManager();
// Create some data
CLevel level = new CLevel();
level.Info = "Testlevel";
dataMng.Datafile.LevelList.Add(level);
// Save to file
dataMng.SaveFile();
// Load from file
dataMng.LoadFile();
So you can do everything in code checked by the compiler. Makes life a lot easier, or what do you think?
Hi all I am having an XML data which is formed in StringBuilder as follows
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-16\"?>");
sb.Append("<TEST>"
+ "<DEMO><CONTENTINFO name=\"Nani\" receiver=\"Lucky\""
+ "/></DEMO></TEST>");
XmlDocument XMLDocument = new XmlDocument();
XMLDocument.LoadXml(sb.ToString());
XmlNodeList nodeList = XMLDocument.FirstChild.ChildNodes;
foreach (XmlNode node in nodeList)
{
}
I tried using XMLDocument to traverse through child nodes to get the data I need to split the data so that it should give name=Nani and receiver=lucky or store the key and value in a dictionary like dic.Add("name","nani") and dic.Add("receiver","lucky") . So can some one help me how to sort it out
If you prefer having a strong set of class instances being formed then use the XmlSerializer and create classes to represent each of the levels of your XML structure.
[XmlRoot("TEST")]
public class Test
{
[XmlElement(Name = "DEMO")]
public Demo Demo
{
get;
set;
}
}
public class Demo
{
[XmlElement("CONTENTINFO")]
public ContentInfo ContentInfo
{
get;
set;
}
}
public class ContentInfo
{
[XmlAttribute(Name = "name")]
public string Name
{
get;
set;
}
[XmlAttribute(Name = "receiver")]
public string Reciever
{
get;
set;
}
}
XmlSerializer serializer = new XmlSerializer(typeof(Test));
serializer.Serialize(....);
Test testInstance = serializer.Deserialize(....);
... etc.
The above code has not been tested but should give you the gist.
Why are you using a StringBuilder to generate your XML? There are much better ways:
var root = new XDocument();
var test = new XElement("TEST");
var demo = new XElement("DEMO");
var contentInfo = new XElement("CONTENTINFO",
new XAttribute("name", "Nani"),
new XAttribute("receiver", "Lucky"));
demo.Add(contentInfo);
test.Add(demo);
root.Add(test);
To pull out the values you need into a dictionary you can use the following LINQ query:
var foo = root.Descendants("DEMO").Elements("CONTENTINFO")
.SelectMany(x => x.Attributes())
.ToDictionary(x => x.Name.ToString(), x => x.Value.ToString());
This will give you a dictionary looking like this:
Key: name =
Value: Nani,
Key: receiver = Value: Lucky