Reputation: 77
I had this question answered before but I changed the form of my xml code to be a little better. I am new at this but I have a question: the following is written in run time. I can add all that is needed but if I want to add elements BEFORE PPE/ppe/UID "A3" I am lost. The following is what I have:
<?xml version="1.0" encoding="utf-8"?>
<Project>
<Details>
<details>
///....more specific data
</details>
</Details>
<Tools />
<Materials />
<PPE>
<ppe>
<UID>A1</UID>
<ppe1Bool>true</ppe1Bool>
<ppe1>Test A1</ppe1>
<ppe2Bool>true</ppe2Bool>
<ppe2>....</ppe2>
</ppe>
<ppe>
<UID>A3</UID>
<ppe1Bool>true</ppe1Bool>
<ppe1>Test A3</ppe1>
<ppe2Bool>true</ppe2Bool>
<ppe2>....</ppe2>
</ppe>
</PPE>
<Video />
<Links />
</Project>
This is what I need to add and it needs to be before A3
<?xml version="1.0" encoding="utf-8"?>
<Project>
<Details>
<details>
///....more specific data
</details>
</Details>
<Tools />
<Materials />
<PPE>
<ppe>
<UID>A1</UID>
<ppe1Bool>true</ppe1Bool>
<ppe1>Test A1</ppe1>
<ppe2Bool>true</ppe2Bool>
<ppe2>....</ppe2>
</ppe>
//////////////////////////////
<ppe>
<UID>A2</UID>
<ppe1Bool>true</ppe1Bool>
<ppe1>New</ppe1>
<ppe2Bool>true</ppe2Bool>
<ppe2>....</ppe2>
</ppe>
/////////////////////////////
<ppe>
<UID>A3</UID>
<ppe1Bool>true</ppe1Bool>
<ppe1>Test A3</ppe1>
<ppe2Bool>true</ppe2Bool>
<ppe2>....</ppe2>
</ppe>
</PPE>
<Video />
<Links />
</Project>
Here's my code and I am close but not quite:
public static void insertRBRowPPE(string strSelection, string strFileName)
{
System.Guid desiredGuid = System.Guid.NewGuid(); //I changed this line to A1..A2..A3 for
//clarity
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);
XmlNode node = xmlDoc.SelectSingleNode("Project/PPE/ppe");
try
{
XElement doc = XElement.Load(strFileName);
foreach (XElement elm in doc.Elements("PPE"))
{
if (node["UID"].InnerText == strSelection)
{
elm.AddBeforeSelf(new XElement("ppe", new XElement("UID", desiredGuid),
new XElement("ppe1Bool", ""), new XElement("ppe1", "New"), new XElement("ppe2Bool", ""),
new XElement("ppe2", "")));
}
}
doc.Save(strFileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
So.. the bugged result is this:
///xml code etc...
<Materials />
<ppe>////////////////////here but I dont want it here
<UID>A2</UID>
<ppe1Bool>true</ppe1Bool>
<ppe1>New</ppe1>
<ppe2Bool>true</ppe2Bool>
<ppe2>....</ppe2>
</ppe>//////////////////////////////
<PPE>
<ppe>
<UID>A1</UID>
<ppe1Bool>true</ppe1Bool>
<ppe1>Test A1</ppe1>
<ppe2Bool>true</ppe2Bool>
<ppe2>....</ppe2>
</ppe>
//////////////////////////////
I want it here....
/////////////////////////////
<ppe>
<UID>A3</UID>
<ppe1Bool>true</ppe1Bool>
<ppe1>Test A3</ppe1>
<ppe2Bool>true</ppe2Bool>
<ppe2>....</ppe2>
</ppe>
</PPE>
<Video />
/// xml code etc....
Any help with all the talent thats out there?? Thanks!!
Upvotes: 0
Views: 66
Reputation: 77
MiMo, thanks! Simple once I see it! I commented out one line from your update and it is good. Thanks again!
//Those who may be interested
//XElement project = doc.Element("Project");
XElement ppe = doc.Element("PPE");
foreach(XElement elm in ppe.Elements("ppe"))
{
if (elm.Element("UID").Value == strSelection)
Upvotes: 0
Reputation: 11973
Adapting the answer to you previous question:
public static void insertRowBeforRowPPE(string strSelection, string strFileName)
{
XElement doc=XElement.Load(strFileName);
XElement project = doc.Element("Project");
XElement ppe = project.Element("PPE");
foreach(XElement elm in ppe.Elements("ppe")
{
if(elm.Element("UID").Value==strSelection)
elm.AddBeforeSelf(new XElement("PPE",new XElement("UID","a2")));
//adds PPE node having UID element with value a2 just before the required node
}
doc.Save(strFileName);
}
Note that you might want to add some more checks - this code would crash if the XML does not contain the Project
element for example.
Upvotes: 1