Adding Subroot elements in XML - c#

I'm making an XML Document which contains subroot nodes. I'm using XmlDocument and adding the child nodes.
This is my code:
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("LICENSE");
if (strGInfo == "")
{
strGInfo = "N/A";
}
XmlElement ginfo = doc.CreateElement("GENERAL_INFO");
ginfo.InnerText = strGInfo;
root.AppendChild(ginfo);
if (strLNo == "")
{
strLNo = "N/A";
}
XmlElement subroot = doc.CreateElement("LICENSE_INFO");
//XmlElement root1 = doc.CreateElement("LICENSE_INFO");
XmlElement lno = doc.CreateElement("LICENCE_NO");
lno.InnerText = txtLNo.Text;
subroot.AppendChild(lno);
if (strUID == "")
{
strUID = "N/A";
}
XmlElement uid = doc.CreateElement("USER_ID");
uid.InnerText = txtUID.Text;
subroot.AppendChild(uid);
if (strOrg == "")
{
strOrg = "N/A";
}
XmlElement org = doc.CreateElement("ORGANIZATION");
org.InnerText = txtOrg.Text;
subroot.AppendChild(org);
if (strUName == "")
{
strUName = "N/A";
}
XmlElement uname = doc.CreateElement("USER_NAME");
uname.InnerText = txtUName.Text;
subroot.AppendChild(uname);
if (strSType == "")
{
strSType = "N/A";
}
XmlElement stype = doc.CreateElement("SOLUTION_TYPE");
stype.InnerText = txtSType.Text;
subroot.AppendChild(stype);
if (strVer == "")
{
strVer = "N/A";
}
XmlElement ver = doc.CreateElement("VERSION");
ver.InnerText = txtVer.Text;
subroot.AppendChild(ver);
XmlElement ltype = doc.CreateElement("LICENCE_TYPE");
ltype.InnerText = drpLType.SelectedItem.Text;
subroot.AppendChild(ltype);
if (strMeapSupp == "")
{
strMeapSupp = "N/A";
}
XmlElement meapsupp = doc.CreateElement("MEAP_SUPPORT");
meapsupp.InnerText = rdoMeapSupport.Text;
subroot.AppendChild(meapsupp);
XmlElement LicFrom = doc.CreateElement("LICENCE_FROM");
LicFrom.InnerText = lblLFrom.Text;
subroot.AppendChild(LicFrom);
XmlElement LicTo = doc.CreateElement("LICENCE_TO");
LicTo.InnerText = lblLTo.Text;
subroot.AppendChild(LicTo);
XmlElement suppfrom = doc.CreateElement("SUPPORT_FROM");
suppfrom.InnerText = lblSuppFrom.Text;
subroot.AppendChild(suppfrom);
XmlElement suppto = doc.CreateElement("SUPPORT_TO");
suppto.InnerText = lblSuppTo.Text;
subroot.AppendChild(suppto);
doc.AppendChild(subroot);
XmlElement subroot2 = doc.CreateElement("LICENCE_CONSTRAINT");
if (strMaxUsr == "")
{
strMaxUsr = "N/A";
}
XmlElement maxusr = doc.CreateElement("MAX_USER");
maxusr.InnerText = txtMaxUsr.Text;
subroot2.AppendChild(maxusr);
if (strMaxMach == "")
{
strMaxMach = "N/A";
}
XmlElement maxmach = doc.CreateElement("MAX_MACHINE");
maxmach.InnerText = txtMaxMach.Text;
subroot2.AppendChild(maxmach);
if (strMachIP == "")
{
strMachIP = "N/A";
}
doc.AppendChild(subroot2);
XmlElement subroot3 = doc.CreateElement("MACHINE_INFO");
XmlElement machip = doc.CreateElement("MACHINE_IP");
machip.InnerText = txtMachIP.Text;
subroot3.AppendChild(machip);
if (strMachMac == "")
{
strMachMac = "N/A";
}
XmlElement machmac = doc.CreateElement("MACHINE_MAC");
machmac.InnerText = txtMachMac.Text;
subroot3.AppendChild(machmac);
doc.AppendChild(subroot3);
XmlElement subroot4 = doc.CreateElement("LICENCE_SIGNATURE");
XmlElement UqID = doc.CreateElement("UNIQUE_ID");
UqID.InnerText = txtUqID.Text;
subroot4.AppendChild(UqID);
doc.AppendChild(subroot4);
doc.Save(#"D:\New.xml");
My XML Document should look something like this:
-<LICENSE>
<GENERAL_INFO> </GENERAL INFO>
-<LICENSE_INFO>
<LICENSE_NO> </LICENSE_NO>
<USER_ID> </USER_ID> //etc
-<LICENCE_CONSTRAINT>
<MAX_USER> </MAX_USER>
<MAX_MACHINE> </MAX_MACHINE>
</LICENCE_CONSTRAINT>
-<MACHINE_INFO>
<MACHINE_IP> </MACHINE_IP>
<MACHINE_MAC> </MACHINE_MAC>
</MACHINE_INFO>
</LICENSE_INFO>
Where am I going wrong?

I would use Linq2Xml for this
string xml = FormXml(licenceNo: "1",machineIP:"1.2.3.4",generalInfo:"some Info");
public string FormXml(
string generalInfo="N/A",
string licenceNo="N/A",
string userID="N/A",
string maxUser="N/A",
string maxMachine="N/A",
string machineIP="N/A",
string machineMAC="N/A")
{
return new XElement("LICENSE",
new XElement("GENERAL_INFO", generalInfo),
new XElement("LICENSE_INFO",
new XElement("LICENSE_NO", licenceNo),
new XElement("USER_ID", userID)),
new XElement("LICENCE_CONSTRAINT",
new XElement("MAX_USER", maxUser),
new XElement("MAX_MACHINE", maxMachine)),
new XElement("MACHINE_INFO",
new XElement("MACHINE_IP", machineIP),
new XElement("MACHINE_MAC", machineMAC))).ToString();
}
and OUTPUT:
<LICENSE>
<GENERAL_INFO>some Info</GENERAL_INFO>
<LICENSE_INFO>
<LICENSE_NO>1</LICENSE_NO>
<USER_ID>N/A</USER_ID>
</LICENSE_INFO>
<LICENCE_CONSTRAINT>
<MAX_USER>N/A</MAX_USER>
<MAX_MACHINE>N/A</MAX_MACHINE>
</LICENCE_CONSTRAINT>
<MACHINE_INFO>
<MACHINE_IP>1.2.3.4</MACHINE_IP>
<MACHINE_MAC>N/A</MACHINE_MAC>
</MACHINE_INFO>
</LICENSE>

you are creating your root element "LICENSE":
XmlElement root = doc.CreateElement("LICENSE");
but you're not appending it to doc.
Furthermore, you are appending to doc multiple times:
doc.AppendChild(subroot);
...
doc.AppendChild(subroot2);
...
doc.AppendChild(subroot3);
which is not possible since an XML document can have only 1 root.
Add your root element like so:
XmlElement root = doc.CreateElement("LICENSE");
doc.AppendChild(root);
And change every doc.AppendChild() lateron into root.AppendChild()
And I have to agree with L.B.: LINQ to XML is much easier for stuff like this (but that wasn't the question ;) )

Related

How to hold more than one values in XML element?

xWaarde.Value is overriding with new values and flushing old values.
how to stored multiple values in Xelement.
private XElement[] AddCoordinatenList(XElement childElements)
{
XmlNode xmlCoordinatenNode = Utility.GetMappingValues(Constants.BMCoordinaten, ConnectionType.BM);
XmlNode xWaardeNode = Utility.GetMappingValues(Constants.BMXwaarde, ConnectionType.BM);
XmlNode yWaardeNode = Utility.GetMappingValues(Constants.BMYwaarde, ConnectionType.BM);
XmlNode CoordinateX = Utility.GetMappingValues(Constants.XCoordinate, ConnectionType.BM);
XmlNode CoordinateY = Utility.GetMappingValues(Constants.YCoordinate, ConnectionType.BM);
var coordinatenList = from document in childElements.DescendantsAndSelf() where document.Name.LocalName == xmlCoordinatenNode.Name select document;
List<XElement> xcoordinatenList = new List<XElement>();
XElement xWaarde = new XElement(CoordinateX.Name);
XElement yWaarde = new XElement(CoordinateY.Name);
if (coordinatenList.Count() > 0)
{
foreach (XElement element in coordinatenList.Descendants())
{
if (element.Name.LocalName == xWaardeNode.Name)
{
xWaarde.Value = element.Value;
}
if (element.Name.LocalName == yWaardeNode.Name)
{
yWaarde.Value = element.Value;
}
}
}
return xcoordinatenList.ToArray();
}
It's not possible. Use Attributes:
XAttribute attribute = new XAttribute("name1", <value>);
element.Add(attribute);
attribute = new XAttribute("name2", <value>);
element.Add(attribute);
You can also add a list of child elements or add all strings as one string, separated by a comma for instance.

Need help retrieving XML data using Linq

I am trying to retrieve data from an XML file and return the parsed data in a list. Depending on what I use to access the data (Element or Attributes) I either get null (in case of Element) or something I cannot decipher (in case of Attributes).
XML Looks like this:
<DATA_RESPONSE>
<HEADER>
<MSGID>IS20101P:091317125610:98::34:0</MSGID>
</HEADER>
<DATA>
<ROW ID='IS20101P' PE_NAME='APP-029' PE_ID='4' CODE='4829' DATA='5,1,500,1' />
<ROW ID='IS20101P' PE_NAME='APPS-029' PE_ID='4' CODE='4829' DATA='4,1,500,1' />
...
</DATA>
<SUMMARY>
</SUMMARY>
<ERRORS>
</ERRORS>
</DATA_RESPONSE>
I am using the following to get the data. I read the file and store XML in a string and call a method with this string as argument:
public static Hashtable GetIDSData(string sXMLString)
{
Hashtable result = new Hashtable();
result.Add("Success", false);
result.Add("ErrorMessage", "");
result.Add("ID", "");
result.Add("PE_NAME", "");
result.Add("PE_ID", "");
result.Add("CODE", "");
result.Add("DATA", "");
xmlDoc.InnerXml = sXMLString;
XmlElement root = xmlDoc.DocumentElement;
XDocument doc = XDocument.Parse(sXMLString);
XmlNode node = xmlDoc.SelectSingleNode("DATA_RESPONSE/DATA");
if (node != null)
{
var AddressInfoList = doc.Root.Descendants("ROW").Select(Address => new
{
ID = Address.Attributes("ID")?.ToString(),
PEName = Address.Attributes("PE_NAME")?.ToString(),
PEID = Address.Attributes("PE_ID")?.ToString(),
Code = Address.Attributes("CODE")?.ToString(),
Data = Address.Attributes("DATA")?.ToString(),
}).ToList();
foreach (var AddressInfo in AddressInfoList)
{
if (string.IsNullOrEmpty(AddressInfo.Code))
{
result["Success"] = false;
result["ErrorMessage"] = "Invalid Code; code is empty.";
}
else
{
result["Success"] = true;
result["ErrorMessage"] = "";
result["ID"] = AddressInfo.ID;
result["PE_NAME"] = AddressInfo.PEName;
result["PE_ID"] = AddressInfo.PEID;
result["CODE"] = AddressInfo.Code;
result["DATA"] = AddressInfo.Data;
}
}
return result;
}
In Linq section, if I use Address.Element("ID").Value, I get null returned.
There is no namespace used in XML.
First off, the GetIDSData() method does not compile as is, because at the line xmlDoc.InnerXml = sXMLString, xmlDoc has not been defined.
I'm assuming you want xmlDoc to be an XmlDocument loaded with the contents of the sXMLString parameter, so I'm changing that line to:
XmlDocument xmlDoc = new XmlDocument {InnerXml = sXMLString};
Also, your root variable is never used, so I removed it for clarity.
Now as for the main part of your question, given your current syntax, you are calling .ToString() on a collection of attributes, which is obviously not what you want. To fix this, when you're iterating the AddressInfoList, You want to fetch the attribute values like:
ID = Address.Attributes("ID")?.Single().Value
or
ID = address.Attribute("ID")?.Value
...rather than Address.Attributes("ID")?.ToString() as you have above.
You are not selecting values of attributes. In your code you are selecting attributes. Not sure what are you trying to achieve, but here is my modified version of your code that loads all elements into DataTable
public static DataTable GetIDSData(string sXMLString)
{
DataTable result = new DataTable();
result.Columns.Add("Success");
result.Columns.Add("ErrorMessage");
result.Columns.Add("ID");
result.Columns.Add("PE_NAME");
result.Columns.Add("PE_ID");
result.Columns.Add("CODE");
result.Columns.Add("DATA");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = sXMLString;
XmlElement root = xmlDoc.DocumentElement;
XDocument doc = XDocument.Parse(sXMLString);
XmlNode node = xmlDoc.SelectSingleNode("DATA_RESPONSE/DATA");
if (node != null)
{
var AddressInfoList = doc.Root.Descendants("ROW").Select(Address => new
{
ID = Address.Attributes("ID").Select(i=>i.Value) ,
PEName = Address.Attributes("PE_NAME").Select(i=>i.Value),
PEID = Address.Attributes("PE_ID").Select(i=>i.Value),
Code = Address.Attributes("CODE").Select(i=>i.Value),
Data = Address.Attributes("DATA").Select(i=>i.Value),
}).ToList();
AddressInfoList.ForEach(e =>
{
e.Code.ToList().ForEach(c =>
{
DataRow row = result.NewRow();
if (!string.IsNullOrEmpty(c))
{
row["Success"] = true;
row["ErrorMessage"] = "";
row["ID"] = e.ID.First();
row["PE_NAME"] = e.PEName.First();
row["PE_ID"] = e.PEID.First();
row["CODE"] = e.Code.First();
row["DATA"] = e.Data.First();
}
else
{
row["Success"] = false;
row["ErrorMessage"] = "Invalid Code; code is empty.";
}
result.Rows.Add(row);
});});
result.Dump();
return result;
}
return result;
}
And this is the result that you will get in your datatable.
ID = Address.Attributes("ID")?.ToString(),
You want to use Attribute(name) (without s) instead:
ID = Address.Attributes("ID")?.Value,

OneNote NavigateTo() responds with hrObjectDoesNotExist(0x80042014) when opening a page

I am trying to programmatically open a one note page. I am doing that in C# and using OneNote2013. I iterate through the notebooks, section groups, sections and finally pages, get a page id and try to open the page with that PageId. But it throws a ObjectDoesNotExist exception. What am I doing wrong? Code Below (OpenOneNote() at the bottom.)
public class OneNoteInterface
{
static Application onenoteApp = new Application();
static XNamespace ns = null;
public static string GetPage(string notebookName, string sectionGroupName, string sectionName, string pageName, out string pageXml, out string sectionId)
{
var onenoteApp = new Application();
pageXml = "";
sectionId = "";
string notebookXml;
onenoteApp.GetHierarchy(null, HierarchyScope.hsChildren, out notebookXml);
var doc = XDocument.Parse(notebookXml);
var ns = doc.Root.Name.Namespace;
foreach (var notebookNode in from node in doc.Descendants(ns +
"Notebook")
select node)
{
//Console.WriteLine(notebookNode.Attribute("name").Value);
if (!notebookNode.Attribute("name").Value.Equals(notebookName))
continue;
string notebookID = notebookNode.Attribute("ID").Value;
onenoteApp.GetHierarchy(notebookID, HierarchyScope.hsChildren, out notebookXml);
Console.WriteLine(notebookXml);
doc = XDocument.Parse(notebookXml);
ns = doc.Root.Name.Namespace;
pageXml = "";
foreach (var sectionGroupNode in from node in doc.Descendants(ns + "SectionGroup") select node)
{
//Console.WriteLine(" " + sectionGroupNode.Attribute("name").Value);
if (sectionGroupNode.Attribute("name").Value.Equals(sectionGroupName))
{
string sectionGroupID = sectionGroupNode.Attribute("ID").Value;
onenoteApp.GetHierarchy(sectionGroupID, HierarchyScope.hsChildren, out notebookXml);
//Console.WriteLine(notebookXml);
doc = XDocument.Parse(notebookXml);
ns = doc.Root.Name.Namespace;
foreach (var sectionNode in from node in
doc.Descendants(ns + "Section")
select node)
{
Console.WriteLine(" " + sectionNode.Attribute("name").Value);
if (sectionNode.Attribute("name").Value.Equals(sectionName))
{
sectionId = sectionNode.Attribute("ID").Value;
onenoteApp.GetHierarchy(sectionId, HierarchyScope.hsChildren, out notebookXml);
//Console.WriteLine(notebookXml);
doc = XDocument.Parse(notebookXml);
ns = doc.Root.Name.Namespace;
foreach (var pageNode in from node in
doc.Descendants(ns + "Page")
select node)
{
//Console.WriteLine(" " + pageNode.Attribute("name").Value);
if (pageNode.Attribute("name").Value.Equals(pageName))
{
string pageId = pageNode.Attribute("ID").Value;
onenoteApp.GetPageContent(pageId, out pageXml, PageInfo.piAll);
doc = XDocument.Parse(pageXml);
//Console.WriteLine(pageXml);
return pageId;
}
}
return null;
}
}
return null;
}
}
return null;
}
return null;
}
static void GetNamespace()
{
string xml;
onenoteApp.GetHierarchy(null, HierarchyScope.hsNotebooks, out xml);
var doc = XDocument.Parse(xml);
ns = doc.Root.Name.Namespace;
}
static string GetObjectId(string parentId, HierarchyScope scope, string objectName)
{
string xml;
onenoteApp.GetHierarchy(parentId, scope, out xml);
var doc = XDocument.Parse(xml);
var nodeName = "";
switch (scope)
{
case (HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;
case (HierarchyScope.hsPages): nodeName = "Page"; break;
case (HierarchyScope.hsSections): nodeName = "Section"; break;
default:
return null;
}
var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();
return node.Attribute("ID").Value;
}
public Boolean OpenOneNote(string notebookName, string sectionGroupName, string sectionName, string pageName)
{
string sectionId = "";
string pageXml = "";
string pageId = GetPage(notebookName, sectionGroupName, sectionName, pageName, out pageXml, out sectionId);
if (pageId != null)
{
onenoteApp.NavigateTo(pageId);
return true;
}
return false;
}

how to get xml nth element name in wpf

public partial class XML_3 : Window
{
public XML_3()
{
this.InitializeComponent();
XmlDocument doc = new XmlDocument();
doc.Load("D:/sample.xml");
XmlNodeList student_list = doc.GetElementsByTagName("Student");
foreach (XmlNode node in student_list)
{
XmlElement student = (XmlElement)node;
int element_count = student.ChildNodes.Count;
}
}
}
In above code.I can get the count of element except root element(Student). now the count is 3.
But i have to get 2ed element name(Kavi),it's attribute element name(ID) and it's child element name(FName,MName).
what should i do to get those stuff.
Please help me...
Use XDocument (why?):
var doc = XDocument.Parse(xml); // OR Load(...)
var nodeCount = doc.Elements().Count();
var secondNode = doc.Elements().Skip(1).First();
var studentName = secondNode.Name;
var studentId = secondNode.Attribute("ID").Value;
or (for your code):
var secondNode = student.ChildNodes[1] as XmlElement;
var studentName = secondNode.LocalName;
var studentId = secondNode.Attributes["ID"];
Added:
var secondNode = student.ChildNodes[1];
var fName =
secondNode.ChildNodes.Cast<XmlElement>().FirstOrDefault(x => x.LocalName == "FName").InnerText;
var mName =
secondNode.ChildNodes.Cast<XmlElement>().FirstOrDefault(x => x.LocalName == "MName").InnerText;
var studentId = secondNode.Attributes["ID"].Value;

Convert xmlstring into XmlNode

i have one xml string like this
string stxml="<Status>Success</Status>";
I also creaated one xml document
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode rootNode = doc.CreateElement("StatusList");
doc.AppendChild(rootNode);
i need an output like this.
<StatusList>
<Status>Success</Status>
</StatusList>
How can i achieve this.if we using innerhtml,it will insert.But i want to insert xml string as a xmlnode itself
A very simple way to achieve what you are after is to use the often overlooked XmlDocumentFragment class:
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode rootNode = doc.CreateElement("StatusList");
doc.AppendChild(rootNode);
//Create a document fragment and load the xml into it
XmlDocumentFragment fragment = doc.CreateDocumentFragment();
fragment.InnerXml = stxml;
rootNode.AppendChild(fragment);
Using Linq to XML:
string stxml = "<Status>Success</Status>";
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("StatusList", XElement.Parse(stxml)));
You could instead use the XElement class:
string stxml = "<Status>Success</Status>";
var status = XElement.Parse(stxml);
var statusList = new XElement("StatusList", status);
var output = statusList.ToString(); // looks as you'd like
If you want to write the new statusList content to a file:
statusList.Save(#"C:\yourFile.xml", SaveOptions.None);
you ca try it using xmlwriter
using (XmlWriter writer = XmlWriter.Create("new.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("StatusList");
writer.WriteElementString("Status", "Success"); // <-- These are new
writer.WriteEndDocument();
}
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using System.ComponentModel;
public class MyClass
{
public static void RunSnippet()
{
XmlNode node = default(XmlNode);
if(node == null)
Console.WriteLine(bool.TrueString);
if(node != null)
Console.WriteLine(bool.FalseString);
XmlDocument doc = new XmlDocument();
node = doc.CreateNode (XmlNodeType.Element,"Query", string.Empty);
node.InnerXml=#"<Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where>";
string xmlData = ToXml<XmlNode>(node);
Console.WriteLine(xmlData);
XmlNode node1 = ConvertFromString(typeof(XmlNode), #"<Query><Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where></Query>") as XmlNode;
if(node1 == null)
Console.WriteLine(bool.FalseString);
if(node1 != null)
Console.WriteLine(bool.TrueString);
string xmlData1 = ToXml<XmlNode>(node1);
Console.WriteLine(xmlData1);
}
public static string ToXml<T>(T t)
{
string Ret = string.Empty;
XmlSerializer s = new XmlSerializer(typeof(T));
using (StringWriter Output = new StringWriter(new System.Text.StringBuilder()))
{
s.Serialize(Output, t);
Ret = Output.ToString();
}
return Ret;
}
public static object ConvertFromString(Type t, string sourceValue)
{
object convertedVal = null;
Type parameterType = t;
if (parameterType == null) parameterType = typeof(string);
try
{
// Type t = Type.GetType(sourceType, true);
TypeConverter converter = TypeDescriptor.GetConverter(parameterType);
if (converter != null && converter.CanConvertFrom(typeof(string)))
{
convertedVal = converter.ConvertFromString(sourceValue);
}
else
{
convertedVal = FromXml(sourceValue, parameterType);
}
}
catch { }
return convertedVal;
}
public static object FromXml(string Xml, Type t)
{
object obj;
XmlSerializer ser = new XmlSerializer(t);
using (StringReader stringReader = new StringReader(Xml))
{
using (System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stringReader))
{
obj = ser.Deserialize(xmlReader);
}
}
return obj;
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
From my experience it is always better to work with unique id's i suggest u look in that situation first and then come back to this page, and research/position my code for a try if u did not yet have a solution for it.
I just finished it on my side for my own project, i have modified abit to look more integrated for your project.
Good luck. Sorry for the late response ;-)
XmlDocument xDoc = new XmlDocument();
string Bingo = "Identification code";
xDoc.Load(pathFile);
XmlNodeList idList = xDoc.GetElementsByTagName("id");
XmlNodeList statusList = xDoc.GetElementsByTagName("Status");
for (int i = 0; i < idList.Count; i++)
{
StatusNode = "<Status>fail</Status>";
XmlDocumentFragment fragment = xDoc.CreateDocumentFragment();
fragment.InnerXml = StatusNode;
statusList[i].InnerXml = "";
statusList[i].AppendChild(fragment);
if (statusList[i].InnerText == Bingo)
{
StatusNode = "<Status>Succes!</Status>";
fragment.InnerXml = Status;
statusList[i].InnerXml = "";
statusList[i].AppendChild(fragment);
}
}
xDoc.Save(pathFile);

Categories