I am for sure missing some important detail here. I just cannot make .NET's XPath work with Visual Studio project files.
Let's load an xml document:
var doc = new XmlDocument();
doc.Load("blah/blah.csproj");
Now execute my query:
var nodes = doc.SelectNodes("//ItemGroup");
Console.WriteLine(nodes.Count); // whoops, zero
Of course, there are nodes named ItemGroup in the file. Moreover, this query works:
var nodes = doc.SelectNodes("//*/#Include");
Console.WriteLine(nodes.Count); // found some
With other documents, XPath works just fine.
I am absolutely puzzled about that. Could anyone explain me what is going on?
You probably need to add a reference to the namespace http://schemas.microsoft.com/developer/msbuild/2003.
I had a similar problem, I wrote about it here. Do something like this:
XmlDocument xdDoc = new XmlDocument();
xdDoc.Load("blah/blah.csproj");
XmlNamespaceManager xnManager =
new XmlNamespaceManager(xdDoc.NameTable);
xnManager.AddNamespace("tu",
"http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode xnRoot = xdDoc.DocumentElement;
XmlNodeList xnlPages = xnRoot.SelectNodes("//tu:ItemGroup", xnManager);
Look at the root namespace; you'll have to include an xml-namespace manager and use queries like "//x:ItemGroup", where "x" is your designated alias for the root namespace. And pass the manager into the query. For example:
XmlDocument doc = new XmlDocument();
doc.Load("my.csproj");
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("foo", doc.DocumentElement.NamespaceURI);
XmlNode firstCompile = doc.SelectSingleNode("//foo:Compile", mgr);
I posted a LINQ / Xml version over at:
http://granadacoder.wordpress.com/2012/10/11/how-to-find-references-in-a-c-project-file-csproj-using-linq-xml/
But here is the gist of it. It may not be 100% perfect......but it shows the idea.
I'm posting the code here, since I found this (original post) when searching for an answer. Then I got tired of searching and wrote my own.
using System;
using System.Linq;
using System.Xml.Linq;
string fileName = #"C:\MyFolder\MyProjectFile.csproj";
XDocument xDoc = XDocument.Load(fileName);
XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
//References "By DLL (file)"
var list1 = from list in xDoc.Descendants(ns + "ItemGroup")
from item in list.Elements(ns + "Reference")
/* where item.Element(ns + "HintPath") != null */
select new
{
CsProjFileName = fileName,
ReferenceInclude = item.Attribute("Include").Value,
RefType = (item.Element(ns + "HintPath") == null) ? "CompiledDLLInGac" : "CompiledDLL",
HintPath = (item.Element(ns + "HintPath") == null) ? string.Empty : item.Element(ns + "HintPath").Value
};
foreach (var v in list1)
{
Console.WriteLine(v.ToString());
}
//References "By Project"
var list2 = from list in xDoc.Descendants(ns + "ItemGroup")
from item in list.Elements(ns + "ProjectReference")
where
item.Element(ns + "Project") != null
select new
{
CsProjFileName = fileName,
ReferenceInclude = item.Attribute("Include").Value,
RefType = "ProjectReference",
ProjectGuid = item.Element(ns + "Project").Value
};
foreach (var v in list2)
{
Console.WriteLine(v.ToString());
}
Related
I need to read following xml file. I've used XML and LINQ but neither of them show any values.I can't find any mistakes on the code.
I've followed This example it's working fine with XML that shown there.
<dataSet>
<transactions>
<trans>1</trans>
<Amount>1000</Amount>
<Name>1000</Name>
<Type>Income</Type>
<Date>2022-04-21T00:00:00+05:30</Date>
</transactions>
</dataSet>
I've use this code.
using System;
using System.Xml;
namespace ReadXMLInCsharp
{
class Program
{
static void Main(string[] args)
{
//create XMLDocument object
XmlDocument xmlDoc = new XmlDocument();
//returns url of main directory which contains "/bin/Debug"
var url = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
//correction in path to point it in Root directory
var mainpath = ("F:\\Education\\Test\\new.xml");
//load xml file
xmlDoc.Load(mainpath);
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/dataSet/transactions");
var NodeStr = "";
foreach (XmlNode node in nodeList)
{
NodeStr = NodeStr + "\nTransaction " + node.SelectSingleNode("trans").InnerText;
}
Console.WriteLine(NodeStr);
}
}
}
I'd note this sort of thing is generally much nicer in the more modern LINQ to XML API. It might look something like this (though there are many ways to skin a cat):
XNamespace ns = "tempuri.org/DataSet.xsd";
var doc = XDocument.Load(mainpath);
var lines =
from dataSet in doc.Elements(ns + "dataSet")
from transactions in dataSet.Elements(ns + "transactions")
from trans in transactions.Elements(ns + "trans")
select $"Transaction {trans.Value}";
var nodeStr = string.Join(Environment.NewLine, lines);
Console.WriteLine(nodeStr);
See this fiddle for a working demo.
Your xml DataSet node has a namespace attribute, so you'll either need to remove it or use XmlNamespaceManager to handle it. Here is a fiddle with both:
https://dotnetfiddle.net/EOXtBN
In the first, I load the xml into a string, then use .Replace:
xmlDoc.LoadXml(getXML().Replace(" xmlns='tempuri.org/DataSet.xsd'", ""));
Probably not optimal, because that namespace is probably there for a reason, but it's possible this could be an option for you. It's the one I got to work first.
Second, I use XmlNamespaceManager to handle the parsing. It doesn't add that much overhead:
xmlDoc.LoadXml(getXML());
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "tempuri.org/DataSet.xsd");
nodeList = xmlDoc.DocumentElement.SelectNodes("//ns:DataSet//ns:transactions", nsmgr);
foreach (XmlNode node in nodeList) {
NodeStr = NodeStr + "\nTransaction " + node.SelectSingleNode("ns:trans", nsmgr).InnerText;
}
Also, bear in mind that you can just include ns:trans in the xpath for the nodelist, like so:
nodeList = xmlDoc.DocumentElement.SelectNodes("//ns:DataSet//ns:transactions//ns:trans", nsmgr);
foreach (XmlNode node in nodeList) {
NodeStr = NodeStr + "\nTransaction " + node.InnerText;
}
Console.WriteLine(NodeStr);
And you can use += to clean it up a little more, too:
nodeList = xmlDoc.DocumentElement.SelectNodes("//ns:DataSet//ns:transactions//ns:trans", nsmgr);
foreach (XmlNode node in nodeList) {
NodeStr += "\nTransaction " + node.InnerText;
}
Console.WriteLine(NodeStr);
Let me know if this works for you.
Your XML file have and you are parsing it via dataset. "s" is in lowercasein your code. set it to
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/dataSet/transactions");
Also check the path to your XML is correct.
I have a xml file:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationConfiguration xmlns="http://test.org/SDK/Configuration.xsd">
<ApplicationName>
<ApplicationUri>123</ApplicationUri>
<ApplicationUri>456</ApplicationUri>
</ApplicationName>
</ApplicationConfiguration>
What I want is set the value of ApplicationUri from 456 to 789 in C# code.
I wrote this code:
string docaddress = "testfile.xml";
XDocument doc = XDocument.Load(docaddress);
doc.Element("ApplicationConfiguration")
.Elements("ApplicationName").FirstOrDefault()
.SetElementValue("ApplicationUri", "789");
doc.Save(docaddress);
The problems are:
There is no error while running. I think the element ApplicationConfiguration is not correct. But when I delete the line xmlns=... from the xml file, it runs normally
The value 789 is replaced with 123, but not 456 as I want (same element name)
Can you tell me how to fix those problems?
Hello and welcome to Stack Overflow!
The xmlns attribute on the ApplicationConfiguration element makes that the root.
So you get the root first. Then you replace the values of each descendant, selecting locally by name with the element name you want. something like this:
string docaddress = "C:\\temp\\testfile.xml";
XDocument doc = XDocument.Load(docaddress);
var root = doc.Root;
var descendants = root.Descendants();
var these = root.Descendants().Where(p => p.Name.LocalName == "ApplicationUri");
foreach (var elem in these)
{
elem.Value = "789";
}
doc.Save(docaddress);
I added namespace to your code :
string docaddress = "testfile.xml";
XDocument doc = XDocument.Load(docaddress);
XNamespace ns = doc.Root.GetDefaultNamespace();
doc.Element(ns + "ApplicationConfiguration")
.Elements(ns + "ApplicationName").FirstOrDefault()
.SetElementValue(ns + "ApplicationUri", "789");
doc.Save(docaddress);
IMHO, here is an easiest method.
It is taking care of the XML default namespace.
No loops. Set based approach.
c#
void Main()
{
const string fileName = #"e:\temp\hala.xml";
const string searchFor = "456";
const string replaceWith = "789";
XDocument doc = XDocument.Load(fileName);
XNamespace ns = doc.Root.GetDefaultNamespace();
// step #1: find element based on the search value
XElement xmlFragment = doc.Descendants(ns + "ApplicationUri")
.Where(d => d.Value.Equals(searchFor)).FirstOrDefault();
// step #2: if found, set its value
if(xmlFragment != null)
xmlFragment.SetValue(replaceWith);
doc.Save(fileName);
}
I have the following XML document to work with.
<ns0:ProductQuantity>
<ns0:Measurement>
<ns0:MeasurementValue>8500</ns0:MeasurementValue>
<ns0:UnitOfMeasureCode Domain="UN-Rec-20">KG</ns0:UnitOfMeasureCode>
</ns0:Measurement>
</ns0:ProductQuantity>
I have yet been unable to figure out how to read the value of "KG" from this document into a string in my C# code. I have extracted other element values, but none of them have this attribute of Domain="UN-Rec->20" attached to them. I am using Visual Studio 2013, and an idea of how I am currently extracting other data is as follows.
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
root = doc.DocumentElement;
prodAmt_xml[1] = root.GetElementsByTagName("ns0:MeasurementValue")[0].InnerText;
If anyone has any incite into this problem and a way to get these 'measurement units' I would be very grateful.
You need to take the XML namespace into account. ns0 isn't really part of the element name, it's a prefix that maps to a namespace (usually a URL).
Using Linq to XML and C# 6, assuming the URL for ns0 is http://foo.bar/:
var doc = XDocument.Load(filePath);
var ns = XNamespace.Get("http://foo.bar/");
var unit =
doc.Descendants(ns + "ProductQuantity")
.Elements(ns + "Measurement")
.Elements(ns + "UnitOfMeasureCode")
.Where(e => e.Attribute("Domain")?.Value == "UN-Rec->20")
.Select(e => e.Value)
.FirstOrDefault();
Note that ?. requires C# 6; if you're using an earlier version, you can also do this:
var doc = XDocument.Load(filePath);
var ns = XNamespace.Get("http://foo.bar/");
var unit =
(from e doc.Descendants(ns + "ProductQuantity")
.Elements(ns + "Measurement")
.Elements(ns + "UnitOfMeasureCode")
let attr = e.Attribute("Domain")
let domain = attr != null ? attr.Value : null
where domain == "UN-Rec->20"
select e.Value).FirstOrDefault();
There is a similar question, but it seems that the solution didn't work out in my case: Weirdness with XDocument, XPath and namespaces
Here is the XML I am working with:
<?xml version="1.0" encoding="utf-8"?>
<Report Id="ID1" Type="Demo Report" Created="2011-01-01T01:01:01+11:00" Culture="en" xmlns="http://demo.com/2011/demo-schema">
<ReportInfo>
<Name>Demo Report</Name>
<CreatedBy>Unit Test</CreatedBy>
</ReportInfo>
</Report>
And below is the code that I thought it should be working but it didn't...
XDocument xdoc = XDocument.Load(#"C:\SampleXML.xml");
XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable());
xnm.AddNamespace(String.Empty, "http://demo.com/2011/demo-schema");
Console.WriteLine(xdoc.XPathSelectElement("/Report/ReportInfo/Name", xnm) == null);
Does anyone have any ideas?
Thanks.
If you have XDocument it is easier to use LINQ-to-XML:
var document = XDocument.Load(fileName);
var name = document.Descendants(XName.Get("Name", #"http://demo.com/2011/demo-schema")).First().Value;
If you are sure that XPath is the only solution you need:
using System.Xml.XPath;
var document = XDocument.Load(fileName);
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("empty", "http://demo.com/2011/demo-schema");
var name = document.XPathSelectElement("/empty:Report/empty:ReportInfo/empty:Name", namespaceManager).Value;
XPath 1.0, which is what MS implements, does not have the idea of a default namespace. So try this:
XDocument xdoc = XDocument.Load(#"C:\SampleXML.xml");
XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable());
xnm.AddNamespace("x", "http://demo.com/2011/demo-schema");
Console.WriteLine(xdoc.XPathSelectElement("/x:Report/x:ReportInfo/x:Name", xnm) == null);
you can use the example from Microsoft - for you without namespace:
using System.Xml.Linq;
using System.Xml.XPath;
var e = xdoc.XPathSelectElement("./Report/ReportInfo/Name");
should do it
To work w/o default namespace suffix, I automatically expand the path.
Usage: SelectElement(xdoc.Root, "/Report/ReportInfo/Name");
private static XElement SelectElement(XElement startElement, string xpathExpression, XmlNamespaceManager namespaceManager = null) {
// XPath 1.0 does not have support for default namespace, so we have to expand our path.
if (namespaceManager == null) {
var reader = startElement.CreateReader();
namespaceManager = new XmlNamespaceManager(reader.NameTable);
}
var defaultNamespace = startElement.GetDefaultNamespace();
var defaultPrefix = namespaceManager.LookupPrefix(defaultNamespace.NamespaceName);
if (string.IsNullOrEmpty(defaultPrefix)) {
defaultPrefix = "ᆞ";
namespaceManager.AddNamespace(defaultPrefix, defaultNamespace.NamespaceName);
}
xpathExpression = AddPrefix(xpathExpression, defaultPrefix);
var selected = startElement.XPathSelectElement(xpathExpression, namespaceManager);
return selected;
}
private static string AddPrefix(string xpathExpression, string prefix) {
// Implementation notes:
// * not perfect, but it works for our use case.
// * supports: "Name~~" "~~/Name~~" "~~#Name~~" "~~[Name~~" "~~[#Name~~"
// * does not work in complex expressions like //*[local-name()="HelloWorldResult" and namespace-uri()='http://tempuri.org/']/text()
// * does not exclude strings like 'string' or function like func()
var s = Regex.Replace(xpathExpression, #"(?<a>/|\[#|#|\[|^)(?<name>\w(\w|[-])*)", "${a}${prefix}:${name}".Replace("${prefix}", prefix));
return s;
}
If anyone has a better solution to find element and attribute names, feel free to change this post.
I have the following XML:
<iq xmlns="jabber:client" to="39850777771287777738178727#guest.google.com/agsXMPP" xml:lang="en" id="sub23" from="search.google.com" type="result">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<subscription subscription="subscribed" subid="5077774B57777BD77770" node="search" jid="39850777771287777738178727#guest.google.com/agsXMPP" />
</pubsub>
</iq>
I've tried parsing with linq to sql, but it doesn't seem to understand that these are different nodes. It groups the whole iq into a single element.
Can anyone help in parsing this using XML?
The data I want to get is the subid="5077774B57777BD77770" and the id="sub23"
Thanks!
Edit:
Here's the code I have, tried doing it in two ways:
XDocument doc = XDocument.Parse("<xml>" + iq.ToString() + "</xml>");
var results = from feed in doc.Elements("xml")
select new
{
Id = (string)feed.Element("iq").Attribute("id"),
Subid = (string)feed.Element("iq").Element("pubsub").Element("subscription").Attribute("subid")
};
and
var doc = new System.Xml.XmlDocument();
doc.LoadXml(iq.ToString());
var searchId = doc.Attributes["id"];
var subid = doc.SelectSingleNode("/pubsub/subscription").Attributes["subid"];
As Dimitre pointed out, you have a namespace issue. This will work:
using System;
using System.Xml;
namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
XmlNamespaceManager namespaces = new XmlNamespaceManager(doc.NameTable);
namespaces.AddNamespace("ns1", "jabber:client");
namespaces.AddNamespace("ns2", "http://jabber.org/protocol/pubsub");
doc.Load("xmltest.xml");
XmlNode iqNode = doc.SelectSingleNode("/ns1:iq", namespaces);
string ID = iqNode.Attributes["id"].Value;
Console.WriteLine(ID);
XmlNode subscriptionNode = doc.SelectSingleNode("/ns1:iq/ns2:pubsub/ns2:subscription", namespaces);
string subID = subscriptionNode.Attributes["subid"].Value;
Console.WriteLine(subID);
Console.ReadLine();
}
}
}
Read this for an explanation and a complete code example how to evaluate an XPath expression that contains location steps with nodes whose names are in a default namespace and are unprefixed in the XML document..
I'm not sure if this is what you're after, but it works:
XNamespace jabber = "jabber:client";
XNamespace pubsub = "http://jabber.org/protocol/pubsub";
string xmltext = "<iq xmlns=\"jabber:client\" to=\"39850777771287777738178727#guest.google.com/agsXMPP\" xml:lang=\"en\" id=\"sub23\" from=\"search.google.com\" type=\"result\">\n"
+ "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">\n"
+ "<subscription subscription=\"subscribed\" subid=\"5077774B57777BD77770\" node=\"search\" jid=\"39850777771287777738178727#guest.google.com/agsXMPP\" />\n"
+ "</pubsub>\n"
+ "</iq>";
XDocument xdoc = XDocument.Parse(xmltext);
var iqelem = xdoc.Element(jabber + "iq");
var id = iqelem.Attribute("id").Value;
var subselem = iqelem.Element(pubsub + "pubsub").Element(pubsub + "subscription");
var subid = subselem.Attribute("subid").Value;
Console.WriteLine("SubId = {0}\nId={1}", subid, id);
I concur with Dimitre - the "empty" xmlns namespace at the top is probably causing the problem. I sometimes strip these out with a regex if they're not used, otherwise play around with XmlNameSpaceManager as described