How to remove the sibling node of XML. I need to remove CreationDateTime value.
I am limited to XMLdocument in C#.
How to use say that i need to select the value element which is next to CreationDateTime Name element.
xmlDoc.Descendants().Where(e => e.Name == "Value").Remove();
<Mic><Attribute>
<Name>CreatedBy</Name>
<Value>MS</Value>
</Attribute>
<Attribute>
<Name>CreationDateTime</Name>
<Value>04/13/2018 19:45:38</Value>
</Attribute></Mic>
Since you are limited to using XmlDocument, you need to get all the Attribute tags and then remove their last child (this is the Value tag).
var doc = new XmlDocument();
doc.Load("YourFile.xml");
// Get all the Attribute tags
var atts = doc.GetElementsByTagName("Attribute");
// Traverse and remove the last child (this is the Value tag)
foreach (XmlNode thisElement in atts)
{
thisElement.RemoveChild(thisElement.LastChild);
}
Related
i am trying to work with http://api.met.no/weatherapi/locationforecast/1.9/?lat=49.8197202;lon=18.1673554 XML.
Lets say i want to select all value attribute of each temperature element.
i tried this.
const string url = "http://api.met.no/weatherapi/locationforecast/1.9/?lat=49.8197202;lon=18.1673554";
WebClient client = new WebClient();
string x = client.DownloadString(url);
XmlDocument xml = new XmlDocument();
xml.LoadXml(x);
XmlNodeList nodes = xml.SelectNodes("/weatherdata/product/time/location/temperature");
//XmlNodeList nodes = xml.SelectNodes("temperature");
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.Attributes[0].Value);
}
But i get nothing all the time. What am i doing wrong?
The current single slash is targeting weatherdata under the root but the root is weatherdata.
Add a preceding slash to your xpath query to make it a double slash:
XmlNodeList nodes = xml.SelectNodes("//weatherdata/product/time/location/temperature");
Double slashes tells xpath to select nodes in the document from the current node that match the selection no matter where they are.
or remove the preceding slash:
XmlNodeList nodes = xml.SelectNodes("weatherdata/product/time/location/temperature");
which looks for the whole path including the root.
Also, since you apparently want the value called value add this:
Console.WriteLine(node.Attributes["value"].Value);
Since the value at of node.Attributes[0].Value may not be in the order you expect.
Are you attempting to loop through each attribute?
foreach (XmlNode node in nodes)
{
//You could grab just the value like below
Console.WriteLine(node.Attributes["value"].Value);
//or loop through each attribute
foreach (XmlAttribute f in node.Attributes)
{
Console.WriteLine(f.Value);
}
}
How do I add another concept node (Concept Key="1234"R) to the child Rssd node where rssd = 3284070
in C#, I'm using XElements to contruct all the XML. Do I need a linq statement?
<Root>
<Rssd Key="3900455" />
<Rssd Key="4442961" />
<Rssd Key="4442961" />
<Rssd Key="4442970" />
<Rssd Key="3284070">
<Concept Key="1662">
<Concept Key="1668">
</Rssd>
</Root>
LINQ is only used to query (to select a part of the dataset), not to modify the dataset. Here, I use it to get the Rssd element where we want to add the new Concept element.
XDocument xDocument = ...
XElement parentElement = (from rssdElement in xDocument.Descendants("Rssd") // Iterates through the collection of all Rssd elements in the document
where rssdElement.Attribute("Key").Value == "3284070" // Filters the elements to get only those which have the correct Key attribute value
select rssdElement).FirstOrDefault(); // Gets the first element that satisfy the above condition (returns null if no element has been found)
if (parentElement == null)
throw new InvalidDataException("No Rssd element found with the key \"3284070\"");
XElement newConceptElement = new XElement("Concept"); // Creates a new Concept element
newConceptElement.Add(new Attribute("Key", "1234")); // Adds an Key attribute to the element with the specified value
parentElement.Add(newConceptElement); // Adds the new Concept element to the Rssd element
XDocument xDOC = XDocument.Load(FilePath);
foreach (XElement xele in xDOC.Descendants("Rssd"))
{
if (xele.Attribute("Key").Value == "3284070")
{
XElement xele1 = XElement.Parse("<Concept Key='1234' />");
xele.Add(xele1);
//Apply a break if you wish to add only one child node else add keep on adding as per your requirement, I am adding only one node
break;
}
}
xDOC.Save(FilePath);
I have the following XML
<ROOT>
<FSM338_Container>
<FSM338_Details>
<RunDate>2013-05-29 09:43:00</RunDate>
<Uic>21690</Uic>
<Date>2013-06-10 00:00:00</Date>
<CASHBREAK>199</CASHBREAK>
<CASHLUNCH>199</CASHLUNCH>
</FSM338_Details>
<FSM338_Details>
<RunDate>2013-05-29 09:43:00</RunDate>
<Uic>21690</Uic>
<Date>2013-06-10 00:00:00</Date>
<CASHBREAK>199</CASHBREAK>
<CASHLUNCH>199</CASHLUNCH>
</FSM338_Details>
</FSM338_Container>
<BillingReport>
<RunDate>2013-05-29 09:43:00</RunDate>
<Uic>21690</Uic>
<Date>2013-06-10 00:00:00</Date>
<gaindacd>1</gaindacd>
<docnum>07000F</docnum>
</BillingReport>
<DataElements>
<unitid>12345</unitid>
<fbocost>0.00</fbo>
</DataElements>
</ROOT>
I need to load the xml doc and add in several elements whenever I find the element named "Uic" . In short if I find "Uic" add in the element <someElement>my stuff here</someElement> at the same level as UIC at all locations.
I'Ve used
XmlDocument xDoc = new XmlDocument();
xDoc.Load(#"path_to_xml.xml");
list = xDoc.GetElementsByTagName("Uic");
I used insertBefore to add in my element but I can get it to copy to only the first element
You can use SelectNodes() method of XmlNode which accepts xpath expression.
XmlNodeList nodes = xDoc.DocumentElement.SelectNodes("Uic");
foreach(XmlNode node in nodes) {
XmlElement element = xDoc.CreateElement("SomeElement");
element.InnerText = "anything";
node.ParentNode.AppendChild(element);
}
i created one xml like this
<names>
<name>
nixon
</name>
</names>
i want iterate each node name by foreach loop or any other loop in silverlight using xdocument.
how can i get this node name?
You can use the DescendantsAndSelf() method of XElement to get all the nodes and their names.
foreach (XElement child in doc.Root.DescendantsAndSelf())
{
Console.WriteLine(child.Name.LocalName);
}
DescendantsAndSelf() Returns a collection of elements that contain this element, and all descendant elements of this element, in document order.
With LinqToXml:
var xDoc = XDocument.Parse(mySmlString);
var names = xDoc.Root.Elements("name").Select(x=> x.Value.Trim()).ToArray();
foreach (var name in names)
{
System.Console.WriteLine(name);
}
How to get a value of XElement without getting child elements?
An example:
<?xml version="1.0" ?>
<someNode>
someValue
<child>1</child>
<child>2</child>
</someNode>
If i use XElement.Value for <someNode> I get "somevalue<child>1</child><child>2<child>" string but I want to get only "somevalue" without "<child>1</child><child>2<child>" substring.
You can do it slightly more simply than using Descendants - the Nodes method only returns the direct child nodes:
XElement element = XElement.Parse(
#"<someNode>somevalue<child>1</child><child>2</child></someNode>");
var firstTextValue = element.Nodes().OfType<XText>().First().Value;
Note that this will work even in the case where the child elements came before the text node, like this:
XElement element = XElement.Parse(
#"<someNode><child>1</child><child>2</child>some value</someNode>");
var firstTextValue = element.Nodes().OfType<XText>().First().Value;
There is no direct way. You'll have to iterate and select. For instance:
var doc = XDocument.Parse(
#"<someNode>somevalue<child>1</child><child>2</child></someNode>");
var textNodes = from node in doc.DescendantNodes()
where node is XText
select (XText)node;
foreach (var textNode in textNodes)
{
Console.WriteLine(textNode.Value);
}
I think what you want would be the first descendant node, so something like:
var value = XElement.Descendents.First().Value;
Where XElement is the element representing your <someNode> element.
You can specifically ask for the first text element (which is "somevalue"), so you could also do:
var value = XElement.Descendents.OfType<XText>().First().Value;