My xml file looks like this:
<Root>
<Child>
<SubChild>
<Item Sequence="2">Value2</Item>
<Item Sequence="1">Value1</Item>
<Node Sequence="1">First</Node>
<Node Sequence="3">Third</Node>
<Node Sequence="2">Second</Node>
<Url>https://url.com</Url>
</SubChild>
<Child>
</Root>
I want my result to be in this order
<Root>
<Child>
<SubChild>
<Item Sequence="1">Value1</Item>
<Item Sequence="2">Value2</Item>
<Node Sequence="1">First</Node>
<Node Sequence="2">Second</Node>
<Node Sequence="3">Third</Node>
<Url>https://url.com</Url>
</SubChild>
<Child>
</Root>
I can get to the node all fine. I am having issues sorting them while maintaining the element position. How can I order the nodes Item and Node and still maintain their order within the SubChild node? I need Items to the first node, followed by Node, and then Url.
This is what I tried.
var xdoc = new XmlDocument();
xdoc.LoadXml(xmlStr);
var doc = XDocument.Parse(xdoc.OuterXml);
var subChild = doc.Descendants("Root").Descendants("Child").Descendants("SubChild");
subChild.Elements("Item").OrderBy(x => Convert.ToInt32(x.Attribute("Sequence")));
subChild.Elements("Node").OrderBy(x => Convert.ToInt32(x.Attribute("Sequence")));
Get the <SubChild> element with XPath: "/Root/Child/SubChild" (subChild). This will be used for the updation later.
Extract the descendants in the <SubChild> element.
Order the result from 1 by LocalName from the element (comparing the element index in the elementOrder array), then by Sequence.
Update the node in subChild via .ReplaceNode().
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
var doc = XDocument.Parse(xmlStr);
var subChild = doc.XPathSelectElement("/Root/Child/SubChild");
var subChildDesc = doc.XPathSelectElement("/Root/Child/SubChild").Descendants();
string[] elementOrder = new string[] { "Item", "Node", "Url" };
subChildDesc = subChildDesc
.OfType<XElement>()
.OrderBy(x => Array.IndexOf(elementOrder, x.Name.LocalName))
.ThenBy(x => Convert.ToInt32(x.Attribute("Sequence")?.Value.ToString()))
.ToList();
subChild.ReplaceNodes(subChildDesc);
Demo # .NET Fiddle
Related
First I want to find and select the PID"5678" from the <Tool>. With help of this PID, i want to find and select the ID"5678" from the <Parent>. The PID and the ID are the same value, but I have to find it from the <Tool> first.
At the moment I have following Code, to select the first PID. How can I "copy" this value and search with them the Attribute "ID"?
List<string> urls = xmldoc2.Descendants("PID").Select(x => x.Attribute("5678").Value).ToList();
<Tools>
<Tools>
<Tool>
<ID>1234</ID>
<PID>5678</PID>
<Name>Test</Name>
</Tool>
</Tools>
<Type>
<Parent>
<ID>5678</ID>
<PID>9999</PID>
<Name>Test2</Name>
</Parent>
</Type>
</Tools>
Notice that your Xml has multiple Root nodes - which does not work well.
So wrap it into single parent node (i.e. "Root" in below example)
Something of this sort should help you.
string xmlData = #"... Your Xml here....";
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlData);
var pidNodes = xmlDoc.SelectNodes("//Root/Tools/Tools/Tool/PID");
foreach(XmlNode node in pidNodes)
{
var typeNodeForPid = xmlDoc.SelectSingleNode(string.Format("//Root/Type/Parent[ID = '{0}']", node.InnerText));
}
I tries to parse a XML file (get it from Dependacy Graph in VS 2012).
Here is sample of my .xml file
<?xml version="1.0" encoding="utf-8"?>
<DirectedGraph xmlns="http://schemas.microsoft.com/vs/2009/dgml">
<Nodes>
<Node Id="#101" Category="CodeSchema_ProjectItem" FilePath="$(ProgramFiles)\windows kits\8.0\include\um\unknwnbase.h" Label="unknwnbase.h" />
<Node Id="#103" Category="CodeSchema_ProjectItem" FilePath="$(ProgramFiles)\windows kits\8.0\include\shared\wtypesbase.h" Label="wtypesbase.h" />
in here, I needs to remove attribute "xmlns" from DirectedGraph.
here's my source to remove this
XmlNodeList rootNode = xmlDoc.GetElementsByTagName("DirectedGraph");
foreach (XmlNode node in rootNode)
{
node.Attributes.RemoveNamedItem("xmlns");
}
but this code doesn't work. If I don't delete this I can't select node like
XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes("/DirectedGraph/Nodes/Node");
What should I do?
If you like you can work with the namespace instead of removing the declaration:
var xml = #"<?xml version=""1.0"" encoding=""utf-8""?>
<DirectedGraph xmlns=""http://schemas.microsoft.com/vs/2009/dgml"">
<Nodes>
<Node Id=""#101"" Category=""CodeSchema_ProjectItem"" FilePath=""$(ProgramFiles)\windows kits\8.0\include\um\unknwnbase.h"" Label=""unknwnbase.h"" />
<Node Id=""#103"" Category=""CodeSchema_ProjectItem"" FilePath=""$(ProgramFiles)\windows kits\8.0\include\shared\wtypesbase.h"" Label=""wtypesbase.h"" />
</Nodes>
</DirectedGraph>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("d", "http://schemas.microsoft.com/vs/2009/dgml");
var nodes = doc.DocumentElement.SelectNodes("/d:DirectedGraph/d:Nodes/d:Node", manager);
Console.WriteLine(nodes.Count);
Use:
private static XElement RemoveAllNamespaces(XElement xmlDocument)
{
if (!xmlDocument.HasElements)
{
XElement xElement = new XElement(xmlDocument.Name.LocalName);
xElement.Value = xmlDocument.Value;
foreach (XAttribute attribute in xmlDocument.Attributes())
xElement.Add(attribute);
return xElement;
}
return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
}
Taken from: How to remove all namespaces from XML with C#?.
You might also want to check out: XmlSerializer: remove unnecessary xsi and xsd namespaces.
I have the following XML and query through the ID,how do get the Parent Hierarchy
<Child>
<Child1 Id="1">
<Child2 Id="2">
<Child3 Id="3">
<Child4 Id="4">
<Child5 Id="5"/>
<Child6 Id="6"/>
</Child4>
</Child3>
</Child2>
</Child1>
</Child>
In this if i query(Id = 4) and find out the Parent elements using Linq in the particular element how to get the following output with Hierarchy.
<Child>
<Child1 Id="1">
<Child2 Id="2">
<Child3 Id="3">
<Child4 Id="4"/>
</Child3>
</Child2>
</Child1>
</Child>
Thanks In Advance.
Assume you want just one node parent tree:
string xml = #"<Child>
<Child1 Id="1">
<Child2 Id="2">
<Child3 Id="3">
<Child4 Id="4">
<Child5 Id="5"/>
<Child6 Id="6"/>
</Child4>
</Child3>
</Child2>
</Child1>
</Child>";
TextReader tr = new StringReader(xml);
XDocument doc = XDocument.Load(tr);
IEnumerable<XElement> myList =
from el in doc.Descendants()
where (string)el.Attribute("Id") == "4" // here whatever you want
select el;
// select your hero element in some way
XElement hero = myList.FirstOrDefault();
foreach (XElement ancestor in hero.Ancestors())
{
Console.WriteLine(ancestor.Name); // rebuild your tree in a separate document, I print ;)
}
To search for every element of your tree iterate retrieve the node with the select query without the where clause and call the foreach for every element.
Based on the sample XML provided, you could walk up the tree to find the parent node once you've found the node in question:
string xml =
#"<Child>
<Child1 Id='1'>
<Child2 Id='2'>
<Child3 Id='3'>
<Child4 Id='4'>
<Child5 Id='5'/>
<Child6 Id='6'/>
</Child4>
</Child3>
</Child2>
</Child1>
</Child>";
var doc = XDocument.Parse( xml );
// assumes there will always be an Id attribute for each node
// and there will be an Id with a value of 4
// otherwise an exception will be thrown.
XElement el = doc.Root.Descendants().First( x => x.Attribute( "Id" ).Value == "4" );
// discared all child nodes
el.RemoveNodes();
// walk up the tree to find the parent; when the
// parent is null, then the current node is the
// top most parent.
while( true )
{
if( el.Parent == null )
{
break;
}
el = el.Parent;
}
In Linq to XML there is a method called AncestorsAndSelf on XElement that
Returns a collection of elements that contain this element, and the
ancestors of this element.
But it will not transform your XML tree the way you want it.
What you want is:
For a given element, find the parent
Remove all elements from parent but the given element
Remove all elements from the given element
Something like this in Linq (no error handling):
XDocument doc = XDocument.Parse("<xml content>");
//finding element having 4 as ID for example
XElement el = doc.Descendants().First(el => el.Attribute("Id").Value == "4");
el.RemoveNodes();
XElement parent = el.Parent;
parent.RemoveNodes();
parent.Add(el);
[Edit]
doc.ToString() must give you what you want as a string.
[Edit]
Using RemoveNodes instead of RemoveAll, the last one also removes attributes.
Removing nodes from the chosen element too.
I found the following way
XElement elementNode = element.Descendants()
.FirstOrDefault(id => id.Attribute("id").Value == "4");
elementNode.RemoveNodes();
while (elementNode.Parent != null)
{
XElement lastNode = new XElement(elementNode);
elementNode = elementNode.Parent;
elementNode.RemoveNodes();
elementNode.DescendantsAndSelf().Last().AddFirst(lastNode);
}
return or Print elementNode.
I am looking for a linq to Xdoc query to group by a subset of the XML nodes. I've only been able to get this working to return a subset of the data but I need the entire xml document passed back with only the particular nodes grouped.
<Root>
<Elementname1>
</Elementname1>
<Elementname2>
</Elementname2>
<Elementname3 attrname="test1">
<Child>
</Child>
</Elementname3>
<Elementname3 attrname="test1">
<Child>
</Child>
</Elementname3>
</Root>
This code:
var result =
from row in xDoc.Descendants("Elementname3")
group row by (string)row.Attribute("attrname") into g
select g.First();
returns:
<Elementname3 attrname="test1">
<Child></Child>
</Elementname3>
Expecting:
<Root>
<Elementname1>
</Elementname1>
<Elementname2>
</Elementname2>
<Elementname3 attrname="test1">
<Child>
</Child>
</Elementname3>
</Root>
I understand since descendant element is starting at elementname3; just not sure on how to expound the linq query to start with the root node and group as expected.
Try this:
var result = new XDocument(
new XElement("Root",
from x in doc.Root.Elements()
group x by new { x.Name, Attr = (string)x.Attribute("attrname") } into g
select g.First()
)
);
I want to remove all text nodes (but not any other type of node) from an XML file. How can I do this?
Example Input:
<root>
<slideshow id="1">
<Image>hii</Image>
<ImageContent>this</ImageContent>
<Thumbnail>is</Thumbnail>
<ThumbnailContent>A</ThumbnailContent>
</slideshow>
<slideshow id="2">
<Image>hii</Image>
<ImageContent>this</ImageContent>
<Thumbnail>is</Thumbnail>
<ThumbnailContent>B</ThumbnailContent>
</slideshow>
</root>
Expected Output:
<root>
<slideshow id="1">
<Image></Image>
<ImageContent></ImageContent>
<Thumbnail></Thumbnail>
<ThumbnailContent></ThumbnailContent>
</slideshow>
<slideshow id="2">
<Image></Image>
<ImageContent></ImageContent>
<Thumbnail></Thumbnail>
<ThumbnailContent></ThumbnailContent>
</slideshow>
</root>
How about:
var doc = XDocument.Load("test.xml");
doc.DescendantNodes()
.Where(x => x.NodeType == XmlNodeType.Text ||
x.NodeType == XmlNodeType.CDATA)
.Remove();
doc.Save("clean.xml");
EDIT: Note that the above was before I realized that XCData derived from XText, leading to the simpler:
var doc = XDocument.Load("test.xml");
doc.DescendantNodes()
.OfType<XText>()
.Remove();
doc.Save("clean.xml");
This question should help: Linq to XML - update/alter the nodes of an XML Document
You can use Linq to open the document and alter the values or remove the nodes altogether.