Can't get to <style> in xml file using XmlDocument - c#

trying to get to <style> to modify it, but I can't get any further than <layouts>, here is my code:
XmlDocument doc = new XmlDocument();
doc.Load(fi.FullName);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("rep", "http://developer.cognos.com/schemas/report/8.0/");
XmlNodeList nodeList = doc.SelectNodes("descendant::rep:layouts", nsmgr);
foreach (XmlNode node in nodeList)
{
Console.WriteLine(node.Name);
//XmlNode styleNode = node.SelectSingleNode("style");
//if (styleNode != null)
// Console.WriteLine(styleNode.InnerText);
}
So, this works ("descendant::rep:layouts"), I get to see (Console.WriteLine=) "layouts". But if I try to get further, even if one node at a time, (descendant::rep:layouts/layout/reportPages/page/pageBody/contents/crosstab/style), there is no single node in the list.. please help!!! My ultimate goal is to modify the "CSS style".
XML file is below (pasted from comment):
<report xmlns= developer.cognos.com/schemas/report/8.0/">
<queries>
<layouts> <layout> <reportPages>
<page name="Page1"> <pageBody> <contents>
<block> <contents> <block> <crosstab name="Crosstab1" refQuery="Query1">
<style>
<CSS value="border-collapse:collapse;font-family:'Times New Roman';border:0.75pt solid black" /> <defaultStyles>
</style>

Most likely all other nodes have some non-empty namespaces (note that empty prefix does not mean "no namespace"). One would need to see XML for better answer.
Yes all your nodes are have "how to ignore namespaces with XPath" set as default namespace, so no nodes have it as prefix. If you want to learn more - click on "xml-namespaces" tag for details. Otherwise use following to ignore namespaces how to ignore namespaces with XPath or prefix node names with namespace prefix as you've done for layouts one:
XmlNode styleNode = node.SelectSingleNode("rep:style", nsmgr);

Related

How to get xml tag which is in DataSet1 tag(C#)? [duplicate]

I'm loading a string into an XML document that contains the following structure:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="clsWorker.cs" />
</ItemGroup>
</Project>
Then I'm loading all into an XmlDocument:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(Xml);
Then the following problem occurs:
XmlNode Node = xmldoc.SelectSingleNode("//Compile"); // returns null
When I remove the xmlns attribute from the root element (Project), it works fine.
How do I get SelectSingleNode to return the relevant element?
You should use an XmlNamespaceManager in your call to SelectSingleNode():
XmlNamespaceManager ns = new XmlNamespaceManager(xmldoc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode node = xmldoc.SelectSingleNode("//msbld:Compile", ns);
Taken right from the documentation of SelectSingleNode() on the MSDN:
Note
If the XPath expression does not include a prefix, it is assumed that the
namespace URI is the empty namespace. If your XML includes a default
namespace, you must still add a prefix and namespace URI to the
XmlNamespaceManager; otherwise, you will not get a node selected. For
more information, see Select Nodes Using XPath Navigation.
And the immediately following sample code is
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);
It's not as if this would be "hidden knowledge". ;-)
This way you don't need to specify namespace:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("your xml");
XmlNode node = xmlDoc.SelectSingleNode("/*[local-name() = 'Compile']");
XmlNode nodeToImport = xmlDoc2.ImportNode(node, true);
xmlDoc2.AppendChild(nodeToImport);
Since the 'ItemGroup' may have multiple 'Compile' children, and you specifically want the 'Compile' children of 'Project/ItemGroup', the following will return all of the desired 'Compile' children and no others:
XmlDocument projectDoc = new XmlDocument();
projectDoc.Load(projectDocPath);
XmlNamespaceManager ns = new XmlNamespaceManager(projectDoc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNodeList xnList = projectDoc.SelectNodes(#"/msbld:Project/msbld:ItemGroup/msbld:Compile", ns);
Note that the 'msbld:' namespace specification needs to precede each node level.

Getting a whole XML node with its markups in a string

Given the following XML example
<aaa>
<bbb id="1">
<ccc att="123"/>
<ccc att="456"/>
<ccc att="789"/>
</bbb>
<bbb id="2">
<ccc att="321"/>
<ccc att="654"/>
<ccc att="987"/>
</bbb>
</aaa>
as an XmlDocument object called xDoc1, I managed to remove the first bbb node thanks to its ID and an XPath instruction, leaving the second bbb node alone in the aaa one.
But now I want to get this removed node and its markups in a single string, as the InnerText value of this node is equal to
<ccc att="123"/><ccc att="456"/><ccc att="789"/>
but I want my string to be equal to
<bbb id='1'><ccc att="123"/><ccc att="456"/><ccc att="789"/></bbb>
How can I do so ? Using XmlDocument is mandatory.
I tried to use the ParentNode method, but then it includes the other bbb node.
My C# code for the moment :
xDoc1 = new XmlDocument();
xDoc1.Load("file.xml"); // Containing the given example above.
XmlNodeList nodes = xDoc1.SelectSingleNodes("//bbb[#id='1']");
foreach (XmlNode n in nodes)
{
XmlNode parent = n.ParentNode;
parent.RemoveChild(n);
}
// At this point, xDoc1 does not contain the first bbb node (id='1') anymore.
Use OuterXml property of XmlNode
xDoc1 = new XmlDocument();
xDoc1.Load("file.xml"); // Containing the given example above.
XmlNodeList nodes = xDoc1.SelectSingleNodes("//bbb[#id='1']");
foreach (XmlNode n in nodes)
{
XmlNode parent = n.ParentNode;
parent.RemoveChild(n);
Console.WriteLine(n.OuterXml);
}
I would firstly suggest not using XmlDocument. It's old tech and has been superseded by XDocument, which gives you Linq2Xml and lots of explicit casting goodness when dealing with attributes etc.
Using the XDocument approach with Linq instead of XPath, it's quite a lot easier to work this problem:
var doc=XDocument.Load("file.xml");
var elToRemove = doc.Root.Elements("bbb").Single(el => (int)el.Attribute("id") == 1);
elToRemove.Remove();
Console.WriteLine(doc.ToString()); //no <bbb id="1">
Console.WriteLine(elToRemove.ToString()); //the full outer text of the removed <bbb>

Modifying an XML node with user-provided namespace and node value

It has taken a while, but I have finally been able to modify an XML document based on user input for the namespace and node name:
string nodeName = "DefinitionName"; // this is really provided by the user
string namespace = "http://schemas.datacontract.org/2004/07/Xxx.Session"; // also user-provided
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(taskResolved.XmlPathAndFileName);
XmlElement rootElement = xmlDocument.DocumentElement;
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
namespaceManager.AddNamespace("snuh", namespace); // hard-coded prefix, grrr...
XmlNodeList xmlNodes;
xmlNodes = rootElement.SelectNodes("//snuh:" + nodeName, namespaceManager);
I feel like I'm doing something wrong because I have to hard-code a prefix (snuh). I could try and choose a prefix, like snuh, that I can hope will never appear in a document, but that isn't foolproof. Another option is to use a GUID for a prefix, but this just seems like a hack work-around. Am I missing something? Is there a better way?
The top of the XML doc looks like this:
<?xml version="1.0" encoding="utf-8"?>
<SessionStateInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1"
z:Type="Xxx.SessionStateInfo"
z:Assembly="Xxx.Common, Version=6.2.0.0, Culture=neutral, PublicKeyToken=null"
xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"
xmlns="http://schemas.datacontract.org/2004/07/Xxx.Session">
<CoaterNumber>25</CoaterNumber>
<DefinitionName z:Id="2">TwoLineMarkerDefinition</DefinitionName>
<EnableManualMode>true</EnableManualMode>
If you want to simply select the first DefinitionName node.
You may write
XmlNode node = rootElement[nodeName, namespace];
and if you want the whole list:
XmlNodeList nodeList = rootElement.GetElementsByTagName(nodeName, namespace);
What about using the XPath local-name() and namespace-uri() functions?
string xpath = string.Format("//*[local-name()='{0}' and namespace-uri()='{1}']", nodeName, namespace);
xmlNodes = rootElement.SelectNodes(xpath);
Don't know if those functions are supported in XmlDocument though, haven't tested it.

Reading Elements within a Namespace

I have an XML file that looks like:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://www.someurl.com/somefile.xslt"?>
<AutoInsuranceClaim xmlns="http://www.someurl.com/schemas/AutoInsuranceClaim">
<Identification>
<BaseOwner>3</BaseOwner>
<BaseType>ABC123</BaseType>
<BaseTypeRef>471038341757</BaseTypeRef>
</Identification>
</AutoInsuranceClaim>
and I'm trying to read the Identification node. Here's my code:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(#"..\..\Data.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "http://www.someurl.com/schemas/AutoInsuranceClaim");
XmlNodeList nodeList = xmlDoc.SelectNodes(#"/ns:AutoInsuranceClaim/Identification", nsmgr);
Console.WriteLine("There are {0} nodes...", nodeList.Count);
I know I should get a least 1 value. My understanding of .NET XML parsing is that if you have a default namespace with no prefix, you have to create your own namespace. But this should have returned 1.
If not, what am I missing?
I might be grasping at straws here, but shouldn't you be namespacing both entities in your xpath expression?
XmlNodeList nodeList = xmlDoc.SelectNodes(#"/ns:AutoInsuranceClaim/ns:Identification", nsmgr);
XElement root = XElement.Load("Data.xml");
var identifications = root.Descendants()
.Where(x => x.Name.LocalName == "Identification")
.ToList()
The problem is that you're trying to find an Identification node without a namespace, but it will have defaulted to the same namespace as the parent due to the xmlns=... part. Try this:
var nodeList = xmlDoc.SelectNodes("/ns:AutoInsuranceClaim/ns:Identification",
nsmgr);
Having tried it myself, it printed a count of 1.
Personally I'd use LINQ to XML instead though, which makes namespace easier handling:
XDocument doc = XDocument.Load(#"..\..\Data.xml");
XNamespace ns = "http://www.someurl.com/schemas/AutoInsuranceClaim";
var nodes = doc.Root.Elements(ns + "Identification");

Get nodes from xml files

How to parse the xml file?
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>link</loc>
<lastmod>2011-08-17T08:23:17+00:00</lastmod>
</sitemap>
<sitemap>
<loc>link</loc>
<lastmod>2011-08-18T08:23:17+00:00</lastmod>
</sitemap>
</sitemapindex>
I am new to XML, I tried this, but it seems to be not working :
XmlDocument xml = new XmlDocument(); //* create an xml document object.
xml.Load("sitemap.xml");
XmlNodeList xnList = xml.SelectNodes("/sitemapindex/sitemap");
foreach (XmlNode xn in xnList)
{
String loc= xn["loc"].InnerText;
String lastmod= xn["lastmod"].InnerText;
}
The problem is that the sitemapindex element defines a default namespace. You need to specify the namespace when you select the nodes, otherwise it will not find them. For instance:
XmlDocument xml = new XmlDocument();
xml.Load("sitemap.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("s", "http://www.sitemaps.org/schemas/sitemap/0.9");
XmlNodeList xnList = xml.SelectNodes("/s:sitemapindex/s:sitemap", manager);
Normally speaking, when using the XmlNameSpaceManager, you could leave the prefix as an empty string to specify that you want that namespace to be the default namespace. So you would think you'd be able to do something like this:
// WON'T WORK
XmlDocument xml = new XmlDocument();
xml.Load("sitemap.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("", "http://www.sitemaps.org/schemas/sitemap/0.9"); //Empty prefix
XmlNodeList xnList = xml.SelectNodes("/sitemapindex/sitemap", manager); //No prefixes in XPath
However, if you try that code, you'll find that it won't find any matching nodes. The reason for this is that in XPath 1.0 (which is what XmlDocument implements), when no namespace is provided, it always uses the null namespace, not the default namespace. So, it doesn't matter if you specify a default namespace in the XmlNamespaceManager, it's not going to be used by XPath, anyway. To quote the relevant paragraph from the Official XPath Specification:
A QName in the node test is expanded into an expanded-name using the
namespace declarations from the expression context. This is the same
way expansion is done for element type names in start and end-tags
except that the default namespace declared with xmlns is not used: if
the QName does not have a prefix, then the namespace URI is null (this
is the same way attribute names are expanded). It is an error if the
QName has a prefix for which there is no namespace declaration in the
expression context.
Therefore, when the elements you are reading belong to a namespace, you can't avoid putting the namespace prefix in your XPath statements. However, if you don't want to bother putting the namespace URI in your code, you can just use the XmlDocument object to return the URI of the root element, which in this case, is what you want. For instance:
XmlDocument xml = new XmlDocument();
xml.Load("sitemap.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("s", xml.DocumentElement.NamespaceURI); //Using xml's properties instead of hard-coded URI
XmlNodeList xnList = xml.SelectNodes("/s:sitemapindex/s:sitemap", manager);
Sitemap has 2 sub nodes "loc" and "lastmod". The nodes that you are accessing are "name" and "url". that is why you are not getting any result. Also in your XML file the last sitemap tag is not closed properly with a corresponding Kindly try xn["loc"].InnerText and see if you get the desired result.
I would definitely use LINQ to XML instead of the older XmlDocument based XML API. You can accomplish what you are looking to do using the following code. Notice, I changed the name of the element that I am trying to get the value of to 'loc' and 'lastmod', because this is what is in your sample XML ('name' and 'url' did not exist):
XElement element = XElement.Parse(XMLFILE);
IEnumerable<XElement> list = element.Elements("sitemap");
foreach (XElement e in list)
{
String LOC= e.Element("loc").Value;
String LASTMOD = e.Element("lastmod").Value;
}

Categories