No problems loading a simple XML file into a LIST. BUT, when I create a second element it loads, but loads everything into one line.
I have even tried using xmlDoc.Descendants("apple") with the same results.
Works.
<?xml version="1.0" encoding="utf-8"?>
<green_apple>
<Location>CA</Location>
<Price>.52</Price>
</green_apple>
XDocument xmlDoc = XDocument.Load("apple.xml");
List<string> list = xmlDoc.Root.Elements()
.Select(element => element.Value.Trim())
.ToList();
List Result:
list[0] = CA
List[1] = .52
Doesn't Work.
<?xml version="1.0" encoding="utf-8"?>
<apple>
<green_apple>
<Location>CA</Location>
<Price>.52</Price>
</green_apple>
<red_apple>
<Location>FL</Location>
<Price>.71</Price>
</red_apple>
</apple>
XDocument xmlDoc = XDocument.Load("apple.xml");
List<string> list = xmlDoc.Root.Elements("green_apple") <<specify specify element.
.Select(element => element.Value.Trim())
.ToList();
List Result:
list[0] = CA.52 <<Here's the problem, they should be in their own list element.
Elements retuns child elements of the current node, which in your case are green_apple elements. So you need to get green_apple child nodes by calling Elements() on green_apple element.
Use this:
List<string> list = xmlDoc.Root.Elements("green_apple").Elements()
.Select(element => element.Value.Trim())
.ToList();
Related
I have a xml file from which only specific nodes have to be removed. The node name will be given as input from the user. How to remove the specific nodes which have requested from the user?
<Customers>
<Customer>
<id>michle</id>
<address>newjersy</address>
</Customer>
<Customer>
<id>ann</id>
<address>canada</address>
</Customer>
</Customers>
I have tried
var customer = new XElement("customer",
from o in customers
select
new XElement("id", id),
new XElement("address", address)
);
Customer will contain a new node
<Customer>
<id>ann</id>
<address>canada</address>
</Customer>
doc.Element("customers").Elements(customer).ToList().Remove();
but this is not working. How can I remove the element from the xml?
Tom,
Try this...
private static void RemoveNode(string sID)
{
XDocument doc = XDocument.Load(#"D:\\Projects\\RemoveNode.xml");
var v = from n in doc.Descendants("Customer")
where n.Element("id").Value == sID
select n;
v.Remove();
doc.Save(#"D:\\Projects\\RemoveNode.xml");
}
This removed one node when I called it using
RemoveNode("michle");
Hope this helps.
Your main mistake is that you are creating new nodes that not attached with the source document instead of retrieving existed nodes from it.
You can use article "Removing Elements, Attributes, and Nodes from an XML Tree" on MSDN as a guideline to manipulating XML data.
For example, use XNode.Remove() method to delete one node from the tree or Extensions.Remove<T>(this IEnumerable<T> source) where T : XNode to remove every node in the source collection of nodes:
doc.Descendants("Customer")
.Where(x => x.Element("id").Value == id)
.Remove();
But you also need to save document via Save method after that for commit your changes:
doc.Save();
You can remove this way based on id
xdoc.Descendants("Customer")
.Where(x => (string)x.Element("id") == "michle")
.Remove();
I want to read a config.xml and put each item into a combobox so that the XML file is the datasource. This is my code, which only gives me one entry in my combobox. How do I separate the keys? This is my filter:
C#
var xmlDocument = XDocument.Load(configfile);
var anredeItems = from key in xmlDocument.Descendants("Anrede")
select key.Value.Trim();
anredeNrComboBox.DataSource = anredeItems.ToList();
This is the XML:
<?xml version="1.0"?>
<Config>
<Anrede>
<key_1>Herrn</key_1>
<key_2>Frau</key_2>
<key_3>Herrn Dr.</key_3>
<key_4>Frau Dr.</key_4>
<key_5>Herrn Dr. Med.</key_5>
</Anrede>
</Config>
Your Descendants("Anrede") query will get you the element Andrede, and reading the Value property of that will return the concatenation of all descendant text nodes, which is what you are seeing in your combo box.
What you want are each of its child element values:
var items - doc.Descendants("Anrede")
.Elements()
.Select(x => x.Value.Trim())
.ToList();
You can change your code like this:
var xmlDocument = XDocument.Load(configfile);
var anredeItems = xmlDocument.Root.Elements("Anrede").Elements().Select(p => p.Value.Trim());
anredeNrComboBox.DataSource = anredeItems.ToList();
I'm trying to use the Bing maps API, which returns an XML document. The document (simplified but keeping structure) is
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
<StatusCode>
200
</StatusCode>
<ResourceSets>
<ResourceSet>
<Resources>
<TrafficIncident>
<Severity>
Minor
</Severity>
<RoadClosed>
false
</RoadClosed>
</TrafficIncident>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>
In this case, there is only 1 traffic issue but there could be many.
I'm trying to extract if the road is closed and the severity
The XML is stored in a xd object (of type XDocuement)
The following works fine (no error but returns all the elements)
var allNodes = (from x in xd.Descendants()
select x).ToList();
but if I add an element name then it returns a list with 0 items
var allNodes = (from x in xd.Descendants("Resources")
select x).ToList();
I thought the above code is saying:
from xd, grab all of the descendants of the "Resources" element
If my understanding is correct, why does it return 0 results
You must include your (default) XML namespace like so:
var name = XName.Get("Resources", "http://schemas.microsoft.com/search/local/ws/rest/v1");
var allNodes = (from x in xd.Descendants(name)
select x).ToList();
You must not forget the XML Namespace.
XNamespace search = "http://schemas.microsoft.com/search/local/ws/rest/v1";
var allNodes = (from x in xd.Descendants(search + "Resources")
select x).ToList();
I am trying to find nodes in an XML document like this:
<?xml version="1.0"?>
<TrainingCenterDatabase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="CyclingTransport">
<Id>2014-07-08T15:28:14Z</Id>
</Activity>
</Activities>
</TrainingCenterDatabase>
I aim to extract the node value 'Id' with code like this:
XDocument doc = XDocument.Load(filePath);
List<string> urlList = doc.Root.Descendants("Id")
.Select(x => (string)x)
.ToList();
Console.WriteLine(urlList.Count);
However the count is 0, where I expect 1.
After some debugging and editing the XML I noticed that if I change the TrainingCenterDatabase node and remove the attributes to this:
<TrainingCenterDatabase>
Then the result is a count of 1 as expected.
So my question is how do I take into account the namespaces so that I can get the value when the TrainingCenterDatabase node has these attributes?
Namespaces in XML can be tricky. I've run into this problem myself a number of times. In all likelihood, the following will fix your problem:
XDocument doc = XDocument.Load(filePath);
List<string> urlList = doc.Root.Descendants(doc.Root.Name.Namespace.GetName("Id"))
.Select(x => (string)x)
.ToList();
Console.WriteLine(urlList.Count);
Basically, this just assumes the underlying element to have the same namespace as your root element. That's true in this case, but of course it doesn't have to be.
The right way, probably, is to do it explicitly. Now, granted, that kind of depends on how you're using this and your datasource, so make the decision for yourself, but that would require doing something more like this:
XDocument doc = XDocument.Load(filePath);
List<string> urlList = doc.Root.Descendants(System.Xml.Linq.XName.Get("Id", "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"))
.Select(x => (string)x)
.ToList();
Console.WriteLine(urlList.Count);
The cause for your problem was that the default behavior for XElement, when not given an explicit namespace, is to assume no namespace. However, the default behavior for the XML spec is to assume the parent's namespace. In your case, those two were different, so it wasn't able to find the descendant.
It Works...
XDocument doc = XDocument.Load(filePath);
XNamespace ns = "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2";
var root = doc.Descendants(ns + "Id").Select(x => x.Value).ToList();
Console.WriteLine(root.Count);
Actullay, I need to get all elements except root node from first xml document and so that I could insert them as child nodes to an element(that has same name as a previous doc's root name) in a new document.
So I have tried various ways to achieve it, one of them is removing the root node of first and then trying to add elements to a new one's as given below:
I have tried the following but could not achieve it.
XDocument testDoc = XDocument.Parse(Mydocument);
testDoc.Descendants().Where(e => e.Name.LocalName == "rootName").Select(m=>m).Single().Remove();
var resultDoc = testDoc;
The above code is giving me an empty "{}" result.
my xml document looks something like the below one's:
<rootName xsi:schemaLocation="" xmlns:xsi="" xmlns="">
<main>
<child>
</child>
<anotherchild>
</anotherchild>
</main>
</rootName>
And another way is getting all the elements of first document as the following:
var resultDoc = testDoc.Descendants(ns + "rootName").Elements();
the above statement is giving me the list of elements in the "testDoc" which
I need to do something like below, I am clueless:
<AnotherDocument xsi:schemaLocation="" xmlns:xsi="" xmlns="">
<firstNode>
<rootName>
<main>
<child>
</child>
<anotherchild>
</anotherchild>
</main>
</rootName>
</firstNode>
Please let me know how to insert those elements in a new document as above if I am correct else let me know the way to resolve this issue.
Thanks in advance.
You can replace content of rootName element in another document with elements from first document root:
var xDoc = XDocument.Parse(Mydocument);
var anotherXDoc = XDocument.Load("anotherdata.xml");
XNamespace ns = "http://..."; // your xml namespance
var rootName = anotherXDoc.Descendants(ns + "rootName").First();
rootName.ReplaceNodes(xDoc.Root.Elements());
By this page_nodes gets all nodes now you can used all node by for each loop
var page_nodes = from p in xdoc.Descendants.Where(e => e.Name.LocalName == "rootName").Select(m=>m).Single().Remove() select p;
foreach (var page_node in page_nodes)
{
//Do stuff
}
Wouldn't removing a root node, remove all its child nodes as well? The result you are getting is to be expected I think. You should probably get all the children of the root and copy them to your new document.