How can i find complexType element knowing it's name(station)?
I'm beginner
Here is an entire xsd code:
http://pastebin.com/ymuPDCCb
This doesn't work.
private XElement GetComplexType(string typeName)
{
XElement complexType = xsdSchema.Elements("complexType")
.Where(a => a.Attributes("name").FirstOrDefault() != null && a.Attribute("name").Value==typeName)
.FirstOrDefault();
return complexType;
}
You have to include the xs namespace and you need to query all levels using Descendants() instead of Elements(), like so:
private static void ParseXml()
{
XDocument doc = XDocument.Load(#"C:\schema.xml");
if (doc != null)
{
XElement nodes = GetComplexType("station", doc);
if (nodes != null)
{
Console.WriteLine("station found...");
}
else
{
Console.WriteLine("station NOT found!!");
}
}
}
private static XElement GetComplexType(string typeName, XDocument xsdSchema)
{
XNamespace ns = "http://www.w3.org/2001/XMLSchema";
XElement complexType = xsdSchema.Descendants(ns + "complexType")
.Where(a => a.Attributes("name").FirstOrDefault() != null && a.Attribute("name").Value == typeName)
.FirstOrDefault();
return complexType;
}
Check this SO answer for more info on Elements vs Descendants
Related
I want to make a funtion to input value (funName) and check XML file attribute (FunName) then output XML file attribute (isEnable) boolean true or false
How can I modify this code?
My XML file
<itema>
<itemb FunName="ABC" isEnable="true"></itemb>
<itemb FunName="DEF" isEnable="false"></itemb>
</itema>
My Code
public bool FunEnable(string funName , string isEnable)
{
bool result = true;
XmlDocument xDL = new XmlDocument();
xDL.Load("C://XMLFile2.xml"); //Load XML file
XmlNode xSingleNode = xDL.SelectSingleNode("//itemb");
XmlAttributeCollection xAT = xSingleNode.Attributes; //read all Node attribute
for (int i = 0; i < xAT.Count; i++)
{
if (xAT.Item(i).Name == "isEnable")
{
Console.WriteLine(xAT.Item(i).Value); //read we want attribute content
}
}
return result;
}
Thanks a lot
Well you can try this :
public static bool FunEnable(string funNam)
{
bool result = true;
XmlDocument xDL = new XmlDocument();
xDL.Load(#"C:/XMLFile2.xml"); //Load XML file
XmlNodeList nodeList = xDL.SelectNodes("//itemb");
foreach (XmlNode node in nodeList)
{
if (node.Attributes["FunName"].Value.Equals(funNam))
{
result = Convert.ToBoolean(node.Attributes["isEnable"].Value);
break;
}
}
Console.WriteLine("with funName = "+ funNam +" isEnable equal to : " + result);
return result;
}
Output
with funName = ABC isEnable equal to : True
This is fairly trivial using LINQ to XML. You can load the document using XDocument.Load and then get your isEnable value like so:
var result = doc.Descendants("itemb")
.Where(e => (string)e.Attribute("FunName") == "ABC")
.Select(e => (bool)e.Attribute("isEnable"))
.Single();
You can see a working demo here: https://dotnetfiddle.net/MYTOl6
var xDoc = XDocument.Load(path);
bool result = (from itemb in xDoc.Descendants("itemb")
where itemb.Attribute("FunName").Value == funcName
select itemb.Attribute("isEnable").Value == "true")
.FirstOrDefault();
Well, I prefer Linq to XML..
Maybe that one works:
public bool FunEnable(string funName, string isEnable)
{
bool result = true;
XDocument xDL = XDocument.Load("C://XMLFile2.xml");
var xSingleNode = from node in xDL.Descendants("itemb")
where node.Attribute("FunName").Value == funName
select node;
if(xSingleNode.Count() > 0)
{
result = xSingleNode.ElementAt(0).Attribute("isEnable").Value == "true";
//If there is at least one node with the given name, result is set to the first nodes "isEnable"-value
}
return result;
}
what is the best way to read xml like this one:
<Users>
<user name = "mail">
<supplier name = "supp1">
<project name = "proj1">
<subscribe name= "sub1"/>
<subscribe name = "sub2"/>
</project>
</supplier>
<supplier name = "supp2">
<project name = "proj2">
<subscribe name = "sub3"/>
</project>
<project name = "proj3">
<subscribe name= "sub4"/>
<subscribe name = "sub5"/>
</project>
<project name = "proj4"/>
</supplier>
<supplier name = "supp3"/>
<supplier name = "supp5">
<project name = "proj4"/>
<supplier name = "supp4"/>
</user>
</Users>
For now I am using
While(reader.Read())
{
if (((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "user")))
break;
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name =="supplier"))
{
foreach (TreeNode tree in TreeView1.Nodes)
{
if (reader.GetAttribute(0) == tree.Text)
{
TreeView1.SelectedNode = tree;
TreeView1.SelectedNode.Checked = true;
Get_projects(reader, tree);
break;
}
}
}
}
this is the main after that is get_projects(...):
private void Get_projects(XmlReader reader, TreeNode tree)
{
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "supplier")) break;
//(reader.IsEmptyElement && reader.Name == "supplier")
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "project"))
{
foreach (TreeNode projTree in tree.Nodes)
{
if (reader.GetAttribute(0) == projTree.Text)
{
TreeView1.SelectedNode = projTree;
TreeView1.SelectedNode.Checked = true;
Get_subscribes(reader, projTree);
break;
}
}
}
}
}
the Get_subscribes(reader, projTree):
private void Get_subscribes(XmlReader reader, TreeNode tree)
{
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "project") ||
(reader.IsEmptyElement && reader.Name == "project")) break;
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "subscribe"))
{
foreach (TreeNode subTree in tree.Nodes)
{
if (reader.GetAttribute(0) == subTree.Text)
{
TreeView1.SelectedNode = subTree;
TreeView1.SelectedNode.Checked = true;
break;
}
}
}
}
}
It doesn't work, so I am wondering is there a better way or what am i missing?
I will give you sample to read properly
<ApplicationPool>
<Accounts>
<Account>
<NameOfKin></NameOfKin>
<StatementsAvailable>
<Statement></Statement>
</StatementsAvailable>
</Account>
</Accounts>
</ApplicationPool>
static IEnumerable<XElement> SimpleStreamAxis(string inputUrl, string elementName)
{
using (XmlReader reader = XmlReader.Create(inputUrl))
{
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == elementName)
{
XElement el = XNode.ReadFrom(reader) as XElement;
if (el != null)
{
yield return el;
}
}
}
}
}
}
using (XmlReader reader = XmlReader.Create(inputUrl))
{
reader.ReadStartElement("theRootElement");
while (reader.Name == "TheNodeIWant")
{
XElement el = (XElement) XNode.ReadFrom(reader);
}
reader.ReadEndElement();
}
Source: Reading Xml with XmlReader in C#
Hope this help.
I would consider the reverse approach i.e.: instead of reading XML and checking if a node exists in TreeView I would prefer to use XPath to check if a node exists in XML document.
In order to do so you have to traverse TreeView nodes and for each node build XPath query
e.g.: /Users/user/supplier[#name='supp1']/project[#name='proj1'].
Having XPath query you can create an instance of XPathDocument based on your XMLReader and run the query. If something is found, you will check current node in TreeView.
you can try XPath to read informations you need
XMLDocument doc = new XMLDocument();
doc.Load(your_xml_file_path);
XMLNodeList list = doc.SelectNodes(#"//project"); //get all project element
There's my code:
private void removeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
if (treeView1.SelectedNode.Parent == null) treeView1.SelectedNode.Remove();
else if (treeView1.SelectedNode.Parent.Nodes.Count == 1) treeView1.SelectedNode.Parent.Remove();
else treeView1.SelectedNode.Remove();
}
XDocument doc = XDocument.Load("test.xml");
if (treeView1.SelectedNode.Parent != null)
{
var xElement = (from q in doc.Elements("dogs").Elements("dog")
where q.Attribute("id").Value == treeView1.SelectedNode.Tag.ToString()
select q);
foreach (var a in xElement)
a.Remove();
doc.Save("test.xml");
}
I want to search through my file for id value and if program will find it, it compares it to tag of selected node, and if it finds it, it'll delete this node. And everytime I'm trying to delete any node, error NullReferenceException was unhandled appears.
When you call:
treeView1.SelectedNode.Remove();
This will either set your treeView1.SelectedNode to null or set the SelectedNode to the removed node's parent or to the next available node.
Also this code will set the parent, of the node you removed, to null. These two cases are likely the root cause of your exception. I would suggest simply setting a temporary variable to point to the node you want removed:
TreeNode node = treeView1.SelectedNode;
treeView1.SelectedNode.Remove();
Then simply change your code to:
TreeNode node = treeView1.SelectedNode;
if (treeView1.SelectedNode != null)
{
if (treeView1.SelectedNode.Parent == null)
treeView1.SelectedNode.Remove();
else if (treeView1.SelectedNode.Parent.Nodes.Count == 1)
treeView1.SelectedNode.Parent.Remove();
else
treeView1.SelectedNode.Remove();
}
XDocument doc = XDocument.Load("test.xml");
var xElement = (from q in doc.Elements("dogs").Elements("dog")
where q.Attribute("id").Value == node.Tag.ToString()
select q);
foreach (var a in xElement)
a.Remove();
doc.Save("test.xml");
How I can make following query
If I have XmlDocument and it may have following xml
<EquipmentParameterModified dateTime="2011-04-06T12:03:10.00+01:00" parameter="ExtApp">
<Extensions ParameterId="External App Interface" FromParameterValue="" ToParameterValue="DISABLED"/>
</EquipmentParameterModified>
How I can check that I have EquipmentParameterModified and take values of ParameterId and ToParameterValue
Thanks for help.
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(new StringReader(xmlstr));
XmlNode node = xmldoc.GetElementsByTagName("Extensions").Item(0);
string id = node.Attributes["ParameterId"].Value;
string val = node.Attributes["ToParameterValue"].Value;
Are you trying to find the element given the 2 input search values? What do you want your output to be? If you just want to see that you have a matching element, this code should do the trick:
If yes, try something like this:
public static void Main()
{
var paramId = "External App Interface";
var toParameterValue = "DISABLED";
var xdoc = XDocument.Parse(#"
<EquipmentParameterModified dateTime='2011-04-06T12:03:10.00+01:00' parameter='ExtApp'>
<Extensions ParameterId='External App Interface' FromParameterValue='' ToParameterValue='DISABLED'/>
</EquipmentParameterModified>");
var ret = xdoc.Root
.Elements("Extensions")
.Where(e => e.Attribute("ParameterId").Value == paramId &&
e.Attribute("ToParameterValue").Value == toParameterValue)
.FirstOrDefault();
if (ret != null)
Console.WriteLine(ret.Name);
}
Update for .NET 2.0 & XmlDocument:
public static void Main()
{
var paramId = "External App Interface";
var toParameterValue = "DISABLED";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(#"
<EquipmentParameterModified dateTime='2011-04-06T12:03:10.00+01:00' parameter='ExtApp'>
<Extensions ParameterId='External App Interface' FromParameterValue='' ToParameterValue='DISABLED'/>
</EquipmentParameterModified>");
XmlNode node = xmlDoc.GetElementsByTagName("Extensions")[0];
if (node.Attributes["ParameterId"].Value == paramId &&
node.Attributes["ToParameterValue"].Value == toParameterValue)
{
Console.WriteLine("Found matching node:" + node.Name);
return;
}
}
I recommend using XPath to get the element you're aiming for, do a null check, then get specific attributes of that element, doing a null check on the attribute value before calling the .Value property.
Is there a simple way, to get all nodes from an xml document? I need every single node, childnode and so on, to check if they have certain attributes.
Or will I have to crawl through the document, asking for childnodes?
In LINQ to XML it's extremely easy:
XDocument doc = XDocument.Load("test.xml"); // Or whatever
var allElements = doc.Descendants();
So to find all elements with a particular attribute, for example:
var matchingElements = doc.Descendants()
.Where(x => x.Attribute("foo") != null);
That's assuming you wanted all elements. If you want all nodes (including text nodes etc, but not including attributes as separate nodes) you'd use DescendantNodes() instead.
EDIT: Namespaces in LINQ to XML are nice. You'd use:
var matchingElements = doc.Descendants()
.Where(x => x.Attribute(XNamespace.Xmlns + "aml") != null);
or for a different namespace:
XNamespace ns = "http://some.namespace.uri";
var matchingElements = doc.Descendants()
.Where(x => x.Attribute(ns + "foo") != null);
see here: Iterating through all nodes in XML file
shortly:
string xml = #"
<parent>
<child>
<nested />
</child>
<child>
<other>
</other>
</child>
</parent>
";
XmlReader rdr = XmlReader.Create(new System.IO.StringReader(xml));
while (rdr.Read())
{
if (rdr.NodeType == XmlNodeType.Element)
{
Console.WriteLine(rdr.LocalName);
}
}
In my opinion the simplest solution is using XPath. Also this works if you have .NET 2:
var testDoc = new XmlDocument();
testDoc.LoadXml(str);
var tmp = testDoc.SelectNodes("//*"); // match every element
XDocument.Descendants will return you all the nodes in a flat enumerable.
Check out LINQ to XML. That does what you need.
http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
You can use the SelectMany extension for example.
But if you want to check the values you can just use LINQ to create where-statements.
public void AddWithChildren(XmlNode xnod, Int32 intLevel) //,XmlDocument xmlDoc
{
List<IEnumerable> item = new List<IEnumerable>();
XmlNode xnodWorking;
String strIndent = new string('-', 2 * intLevel);
String strIndent1 = new string('#', 2 * intLevel);
if (xnod.NodeType == XmlNodeType.Element)
{
item.Add(new ListXML(strIndent + xnod.Name, strIndent + xnod.Name, ""));
XmlNamedNodeMap mapAttributes = xnod.Attributes;
foreach (XmlNode xnodAttribute in mapAttributes)
{
item.Add(new ListXML(strIndent1 + xnodAttribute.Name, strIndent1 + xnodAttribute.Name, ""));
}
if (xnod.HasChildNodes)
{
xnodWorking = xnod.FirstChild;
while (xnodWorking != null)
{
AddWithChildren(xnodWorking, intLevel + 1);
xnodWorking = xnodWorking.NextSibling;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument document = new XmlDocument();
string xmlStr;
using (var wc = new WebClient())
{
xmlStr = wc.DownloadString("test.xml");
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
XmlNode xnod = xmlDoc.DocumentElement;
AddWithChildren(xnod, 1);
}
string AttrNameerr = "err";//find error code in xml
XmlReader rdr = XmlReader.Create(new stem.IO.StringReader(somesXMLtring));//somesXMLtring is xml in string variable we want to find attribute in.
while (rdr.Read())
{
if (rdr.NodeType == XmlNodeType.Element)
{
//Found the new element, now check if the required attribute is present or not. if not, ignore, if yes then display the same
string val = rdr.GetAttribute(AttrNameerr);//AttrNameerr is name of attribute we need to get value of which. here we are searching for error code stored as value of 'err' attribute
if (val != null)
textBox.Text = strResult = "error = " + rdr.GetAttribute(AttrNameerr);
}
}