hi guys i have a problem with the treeview control in asp.net. I want to fill that with a xml file but the treeview in asp.net is diffrent to the window form treeview control :/
The name of my treeview is treeview...i don't have the xml in a folder I create a xml string and use this but in this example i only want test how i can fill this treeview out of code. how i can do this with my method?
here is my code:
XElement doc = XElement.Load("~/App_Data/test_xml.xml");
TreeNode Feature;
TreeNode User;
foreach (XElement state in doc.Descendants("FEATURE"))
{
Feature = treeview.Nodes.Add(state.Attribute("NAME").Value);
foreach (XElement region in state.Descendants("USER"))
{
User =
Feature.Nodes.Add(region.Attribute("NAME").Value);
foreach (XElement area in region.Descendants("NAME"))
{
User.Nodes.Add(area.Attribute("NAME").Value);
}
}
}
my xml example:
<?xml version="1.0" encoding="UTF-8"?>
<LM-X STAT_VERSION="3.32">
<LICENSE_PATH TYPE="NETWORK" HOST="Server002" SERVER_VERSION="4.4.4" UPTIME="53 day(s) 21 hour(s) 10 min(s) 50 sec(s)">
<FEATURE NAME="GlobalZoneEU" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="111720" TOTAL_LICENSES="147000" SHARE="CUSTOM ,VIRTUAL">
<USER NAME="SYSTEM" HOST="Server1" IP="" USED_LICENSES="2000" LOGIN_TIME="2013-04-17 12:42" CHECKOUT_TIME="2013-04-17 12:42" SHARE_CUSTOM=""/>
>
<USER NAME="Admin" HOST="Server1" IP="" USED_LICENSES="720" LOGIN_TIME="2013-04-17 12:44" CHECKOUT_TIME="2013-04-17 12:44" SHARE_CUSTOM=""/>
>
<USER NAME="Test.A" HOST="4327" IP="" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 07:52" CHECKOUT_TIME="2013-05-21 07:52" SHARE_CUSTOM=""/>
>
<USER NAME="Test.B" HOST="4327" IP="" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 07:54" CHECKOUT_TIME="2013-05-21 07:54" SHARE_CUSTOM=""/>
>
<USER NAME="Test.C" HOST="4970" IP="" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:15" CHECKOUT_TIME="2013-05-21 08:15" SHARE_CUSTOM=""/>
</FEATURE>
<FEATURE NAME="HWAIFPBS" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
<FEATURE NAME="HWAWPF" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
<FEATURE NAME="HWAcuconsole" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
</LICENSE_PATH>
</LM-X>
You can use XmlDataSource as Jeremy suggested or change code as below
XElement doc = XElement.Load("~/App_Data/test_xml.xml");
TreeNode root = new TreeNode("FEATURES");
foreach (XElement state in doc.Descendants("FEATURE"))
{
TreeNode feature = new TreeNode(state.Attribute("NAME").Value);
foreach (XElement region in state.Descendants("USER"))
{
TreeNode user = new TreeNode(region.Attribute("NAME").Value);
foreach (XElement area in region.Descendants("NAME"))
{
user.ChildNodes.Add(new TreeNode(area.Attribute("NAME").Value));
}
feature.ChildNodes.Add(user);
}
root.ChildNodes.Add(feature);
}
treeview.Nodes.Add(root);
Related
this is part of my xml file:
<PipeSelectionOptions ID="9" ClassId="108" CoTo="ODRDaneOgolneCOOP" AlreadyDumped="False">
<DobierajKsztalki Caption="Dobieraj ksztaĆtki" Value="A">True</DobierajKsztalki>
<KorygujSrednice Caption="koryguj" Value="B">True</KorygujSrednice>
<PonownieDobierajSrednice Caption="P">True</PonownieDobierajSrednice>
<PrzechodzDoNastepcowRur Caption="P">True</PrzechodzDoNastepcowRur>
<SposobWymiarowaniaWentObjesc Caption="S" Value="A">N</SposobWymiarowaniaWentObjesc>
<ZachowajNarzuconeSrednice Caption="Z">True</ZachowajNarzuconeSrednice>
<NastepcyRur Count="2">
<ODRDaneOgolneNastepcyRurItem Index="0" ID="0" ClassId="0" AlreadyDumped="False">
<IsAutoMetkaNastepnika Caption="">False</IsAutoMetkaNastepnika>
<Metka Caption="" Value="R">2</Metka>
<MetkaNastepnika Caption="" Value="R">1</MetkaNastepnika>
</ODRDaneOgolneNastepcyRurItem>
<ODRDaneOgolneNastepcyRurItem Index="1" ID="3" ClassId="0" AlreadyDumped="False">
<IsAutoMetkaNastepnika Caption="">True</IsAutoMetkaNastepnika>
<Metka Caption="" Value="R">1</Metka>
<MetkaNastepnika Caption="" Value="(brak)">0</MetkaNastepnika>
</ODRDaneOgolneNastepcyRurItem>
</NastepcyRur>
I want get to ODRDaneOgolneNastepcyRurItem by code:
foreach (XElement context in rootTemplate.XPathSelectElements("//PipeSelectionOptions"))
{
if (context.Attribute("CoTo").Value.ToString().Equals("ODRDaneOgolneCOOP"))
{
foreach (var NastepcyRur in context.Descendants())
{
if (NastepcyRur.Name.LocalName.Equals("ODRDaneOgolneNastepcyRurItem"))
{
sb.Append("<li><span>Rodzina rur:</span> " + NastepcyRur.Element("Metka").Attribute("Value").Value.ToString() + ";<span> Nastpca rur:</span> " + NastepcyRur.Element("MetkaNastepnika").Attribute("Value").Value.ToString() +"</li>");
}
}
}
}
..but foreach (second) finish on ZachowajNarzuconeSrednice, don't see NastepcyRur and children of NastepcyRur . Why?
I am trying to update a xml on basis of id.
my xml file looks like this
<?xml version="1.0" standalone="yes"?>
<CATALOG>
<CD>
<ID>0</ID>
<HeaderDetailID>0</HeaderDetailID>
<FirstName>
</FirstName>
<LastName>
</LastName>
<EmployeeID>
</EmployeeID>
<Department>
</Department>
<Postion>
</Postion>
<Application>
</Application>
<Filter>
</Filter>
<AreaorCountryorStation>
</AreaorCountryorStation>
<NetworkDomain>
</NetworkDomain>
<Action>
</Action>
<NameOfController>
</NameOfController>
<Status>
</Status>
</CD>
<CD>
<ID>1</ID>
<HeaderDetailID>1</HeaderDetailID>
<FirstName>Basant</FirstName>
<LastName>Basant</LastName>
<EmployeeID>Basant</EmployeeID>
<Department>Basant</Department>
<Postion>Basant</Postion>
<Application>
</Application>
<Filter>
</Filter>
<AreaorCountryorStation>Basant</AreaorCountryorStation>
<NetworkDomain>Basant</NetworkDomain>
<Action>
</Action>
<NameOfController>Basant</NameOfController>
<Status>Request Completed</Status>
</CD>
</CATALOG>
Now I try to fetch id from hidden field and get It on server side to update the node name status.
for (int i = 0; i < DateData.Length - 1; i++)
{
string iRecordID = DateData[i];
XmlDocument xmlDoc = new XmlDocument();
string filepathsUpdate = Server.MapPath("Contact.xml");
xmlDoc.Load(filepathsUpdate);
XmlNode node = xmlDoc.SelectSingleNode("/CATALOG/CD[ID=" + DateData[i] + "]/Status");
node.InnerText = "Request Completed";
xmlDoc.Save(filepathsUpdate);
}
It works locally fine but on server it doesn't work.
Can I close the connection in XML file
I feel like xmlDoc.Save not working
I have simple loop for finding child Element:
XDocument xDoc = XDocument.Load(#"bpmn.xml");
//Run query
foreach (XElement level1Element in xelement.Elements("process"))
{
System.Diagnostics.Debug.WriteLine(level1Element.Attribute("id").Value);
foreach (XElement level2Element in level1Element.Elements("task"))
{
System.Diagnostics.Debug.WriteLine(
" " + level2Element.Attribute("name").Value);
}
}
Why don't I get any output?
The input XML:
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://sourceforge.net/bpmn/definitions/_1384266732154" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:yaoqiang="http://bpmn.sourceforge.net" exporter="Yaoqiang BPMN Editor" exporterVersion="2.2.18 (GPLv3, Non-Commercial)" expressionLanguage="http://www.w3.org/1999/XPath" id="_1384266732154" name="" targetNamespace="http://sourceforge.net/bpmn/definitions/_1384266732154" typeLanguage="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL http://bpmn.sourceforge.net/schemas/BPMN20.xsd"> <collaboration id="COLLABORATION_1" isClosed="false">
<participant id="_2" name="CEO" processRef="PROCESS_1">
<participantMultiplicity maximum="1" minimum="0"/>
</participant>
<participant id="_3" name="Manager" processRef="PROCESS_2">
<participantMultiplicity maximum="1" minimum="0"/>
</participant>
<participant id="_4" name="project Manager" processRef="PROCESS_3">
<participantMultiplicity maximum="1" minimum="0"/>
</participant>
<participant id="_5" name="HR" processRef="PROCESS_4">
<participantMultiplicity maximum="1" minimum="0"/>
</participant>
<participant id="_6" name="Employee" processRef="PROCESS_5">
<participantMultiplicity maximum="1" minimum="0"/>
</participant> </collaboration> <process id="PROCESS_1" isClosed="false" isExecutable="true" processType="None">
<startEvent id="_8" isInterrupting="true" name="Start Event" parallelMultiple="false">
<outgoing>_12</outgoing>
</startEvent>
<task completionQuantity="1" id="_9" isForCompensation="false" name="Task" startQuantity="1">
<incoming>_12</incoming>
<outgoing>_13</outgoing>
</task>
<sendTask completionQuantity="1" id="_10" implementation="##WebService" isForCompensation="false" name="Send Task" startQuantity="1">
<incoming>_13</incoming>
<outgoing>_14</outgoing>
</sendTask>
<task completionQuantity="1" id="_11" isForCompensation="false" name="Task" startQuantity="1">
<incoming>_14</incoming>
</task>
<sequenceFlow id="_12" sourceRef="_8" targetRef="_9"/>
<sequenceFlow id="_13" sourceRef="_9" targetRef="_10"/>
<sequenceFlow id="_14" sourceRef="_10" targetRef="_11"/> </process> <process id="PROCESS_2" isClosed="false" isExecutable="true" processType="None">
<task completionQuantity="1" id="_17" isForCompensation="false" name="Task" startQuantity="1">
<outgoing>_21</outgoing>
</task>
<task completionQuantity="1" id="_19" isForCompensation="false" name="Task" startQuantity="1">
<incoming>_21</incoming>
<outgoing>_23</outgoing>
<outgoing>_25</outgoing>
</task>
<sequenceFlow id="_21" sourceRef="_17" targetRef="_19"/>
<receiveTask completionQuantity="1" id="_22" implementation="##WebService" instantiate="false" isForCompensation="false" name="Receive Task" startQuantity="1">
<incoming>_23</incoming>
</receiveTask>
<sequenceFlow id="_23" name="go on" sourceRef="_19" targetRef="_22"/>
<task completionQuantity="1" id="_24" isForCompensation="false" name="Task" startQuantity="1">
<incoming>_25</incoming>
</task>
<sequenceFlow id="_25" sourceRef="_19" targetRef="_24"/> </process> </definitions>
You need to get the root element first, by xDoc.Root (if xelement in your code is xDoc)
(The main problem here) Your XML has a custom namespace, in which case you need to specify it when calling Elements().
Putting together:
XDocument xDoc = XDocument.Load(#"bpmn.xml");
var ns = XNamespace.Get("http://www.omg.org/spec/BPMN/20100524/MODEL");
//Run query
foreach(XElement level1Element in xDoc.Root.Elements(ns + "process"))
{
System.Diagnostics.Debug.WriteLine(level1Element.Attribute("id").Value);
foreach(XElement level2Element in level1Element.Elements(ns + "task"))
{
System.Diagnostics.Debug.WriteLine(" " + level2Element.Attribute("name").Value);
}
}
You need to select from the root element. Try changing:
xelement.Elements("process"))
To:
xDoc.Elements("definitions").Elements("process")
Your .xml file provided with namespace, still you should use it in your code. Please try this:
XDocument xDoc = XDocument.Load(#"xmlfile1.xml");
//Run query
XNamespace ns = "http://www.omg.org/spec/BPMN/20100524/MODEL";
foreach (XElement level1Element in xDoc.Descendants(ns + "process"))
{
System.Diagnostics.Debug.WriteLine(level1Element.Attribute("id").Value);
foreach (XElement level2Element in level1Element.Elements(ns + "task"))
{
System.Diagnostics.Debug.WriteLine(
" " + level2Element.Attribute("name").Value);
}
}
Also, in your case use Descendants() method instead of Elements().
More info http://msdn.microsoft.com/en-us/library/system.xml.linq.xname(v=vs.110).aspx
first, sorry for my bad english, i'm learning yet. So, I have to delete specifics nodes of a xml file according with their attributes. This is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<Lista>
<Indice value="8">
<Palavra value="casa" />
<Significados>s1,,,,</Significados>
</Indice>
<Indice value="49">
<Palavra value="teste" />
<Significados>1,2,,,</Significados>
</Indice>
<Indice value="72">
<Palavra value="cristiano" />
<Significados>ornelas,ribeiro,,,</Significados>
</Indice>
<Indice value="72">
<Palavra value="teste2" />
<Significados>s2,s3,,,</Significados>
</Indice>
</Lista>
I have to delete all Indice nodes and your childrens that have the attribute value="72" for example. How can I do that? The language is c# and the xml file after of delete must stay in this form:
<?xml version="1.0" encoding="utf-8"?>
<Lista>
<Indice value="8">
<Palavra value="casa" />
<Significados>s1,,,,</Significados>
</Indice>
<Indice value="49">
<Palavra value="teste" />
<Significados>1,2,,,</Significados>
</Indice>
</Lista>
XDocument xdoc=XDocument.Parse(xmlStr); //or XDocument.Load
var matchingElements = xdoc.Root
.Descendants("Indice")
.Where(e => (int)e.Attribute("value") == 72)
.ToList();
foreach(var elem in matchingElements)
{
elem.Remove();
}
xdoc.Save(newFileName);
saves the following doc:
<Lista>
<Indice value="8">
<Palavra value="casa" />
<Significados>s1,,,,</Significados>
</Indice>
<Indice value="49">
<Palavra value="teste" />
<Significados>1,2,,,</Significados>
</Indice>
</Lista>
This is an alternative to Spender although he should get the question answered if his works.
XmlDocument doc = new XmlDocument();
doc.Load("xml path");
XmlNode node = doc.SelectSingleNode("/Lista");
foreach (XmlNode nodes in node.SelectNodes("Indice/#value"))
{
if (nodes.Value == "72")
{
nodes.RemoveAll();
}
}
i am experimenting with C# and xml, i am trying to read a XML file i want to validate if "NumberOfDays" , "NumberOfBooks", "NumberOfExam", "CurrentDate" are existing, if missing. i want to add them with there values.
i have the following xmldocument :
<?xml version="1.0" encoding="utf-8" ?>
<MySample>
<Content>
<add key="NumberOfDays" value="31" />
<add key="NumberOfBooks" value="20" />
<add key="NumberOfExam" value="6" />
<add key="CurrentDate" value="15 - Jul - 2011" />
</Content>
</MySample>
i am writing a sample application in c# using
--------Edit--------
thank you AresAvatar for your responce.
but if the value exists i would like to update its value i.e. if let's say
<add key="NumberOfExam" value="" />
i want to change the value to 6
You can get a list of which nodes exist like this:
// Get a list of which nodes exist
XmlDocument doc = new XmlDocument();
doc.LoadXml(myXml);
List<string> existingNodes = new List<string>();
XmlNodeList bookNodes = doc.SelectNodes("/MySample/Content/add");
foreach (XmlNode nextNode in bookNodes)
{
foreach (XmlAttribute nextAttr in nextNode.Attributes)
existingNodes.Add(nextAttr.InnerText);
}
You can add missing nodes like this:
// Add nodes
XmlNode addRoot = doc.SelectSingleNode("/MySample/Content");
XmlElement addme = doc.CreateElement("add");
addme.SetAttribute("NumberOfDays", "31");
addRoot.AppendChild(addme);
You can set the value of existing nodes like this:
// Update a node
foreach (XmlNode nextNode in bookNodes)
{
foreach (XmlAttribute nextAttr in nextNode.Attributes)
{
switch (nextAttr.Name)
{
case "NumberOfDays":
((XmlElement)nextNode).SetAttribute("value", "31");
break;
// etc.
}
}
}
First of all, if you have control over the generated XML (if you make it yourself), avoid using this schema:
<?xml version="1.0" encoding="utf-8" ?>
<MySample>
<Content>
<add key="NumberOfDays" value="31" />
<add key="NumberOfBooks" value="20" />
<add key="NumberOfExam" value="6" />
<add key="CurrentDate" value="15 - Jul - 2011" />
</Content>
</MySample>
It's much easier to use with that schema:
<?xml version="1.0" encoding="utf-8" ?>
<MySample>
<Content>
<Adds>
<NumberOfDays>31<NumberOfDays/>
<NumberOfBooks>20<NumberOfBooks/>
<NumberOfExam>6<NumberOfExam/>
<CurrentDate>5 - Jul - 2011<CurrentDate/>
</Adds>
</Content>
</MySample>
And then:
XmlDocument doc = new XmlDocument();
doc.Load("YourXmlPath");
XmlNode firstNode = doc["MySample"];
if(firstNode != null)
{
XmlNode secondNode = firstNode["Content"];
if(secondNode != null)
{
XmlNode thirdNode = secondNode["Adds"];
if(thirdNode != null)
{
if(thirdNode["NumberOfDays"] == null) //The "NumberOfDays" node does not exist, we create it.
{
XmlElement newElement = doc.CreateElement("NumberOfDays");
newElement.InnerXml = 31;
thirdNode.AppendChild(newElement);
}
//Do the same for the other nodes...
}
}
}
doc.Save("YourXmlPath");
Just remember to check for null on every node, or put the whole block into a try/catch.
And to save the XML once you did your changes.
XmlDocument.Load() function loads the XML in memory, so you can check for any node without making "blind loops".