How to process webdav server XML request in C# - c#

I am writing a custom WebDAV server in C#. One of the client test programs I am using is NetDrive and it claims and appears to be a WebDAV compliant client. My problem is I am receiving a request on the server in the following format:
<?xml version="1.0" encoding="utf-8"?>
<propfind xmlns="DAV:">
<allprop/>
</propfind>
But other clients do this:
<?xml version="1.0" encoding="utf-8"?>
<D:propfind xmlns:D="DAV:">
<D:allprop/>
</D:propfind>
The two different namespace formats keep on fooing up my logic to look for the "allprop" element. My code looks a bit like this:
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><propfind xmlns=\"DAV:\"><allprop/></propfind>"; //Hardcode to make all the StackOverflow users' lives easier
XPathDocument doc = new XPathDocument(new StringReader(xml));
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator it = nav.Select("/propfind/*");
Now, I know I need to put in some type of namespace manager for the "DAV:", so I tried this:
XmlNamespaceManager nsman = new XmlNamespaceManager(nav.NameTable);
nsman.AddNamespace("", "DAV");
XPathNodeIterator it = nav.Select("/propfind/*", nsman);
But I'm getting no nodes in my iterator for the first XML file. It seems the default namespace isn't working like I thought it should.
What am I doing wrong? How do I query this XML for the existence of an allprop node when the namespace may be the default, or may be explicitly named?

You are using the wrong namespace in your code. Unforunalely the WebDAV specs use
'DAV:' as the namespace for WebDAV nodes and attributes (this seems to be caused by
a missunderstanding of the XML namespace mechanism).

I ended up looking for the namespace URI (DAV:) and adding it if it didn't exist. Then I just did a namespace-qualified SELECT and it worked in all my test cases:
XPathDocument document = new XPathDocument(xml);
XPathNavigator navigator = document.CreateNavigator();
//Get namespaces & add them to the search
bool hasDAV = false;
string davPrefix = "D";
XmlNamespaceManager nsman = new XmlNamespaceManager(navigator.NameTable);
foreach (KeyValuePair<string, string> nskvp in navigator.GetNamespacesInScope(XmlNamespaceScope.All))
{
if (string.Compare(nskvp.Value, "DAV:", StringComparison.InvariantCultureIgnoreCase) == 0)
{
hasDAV = true;
davPrefix = nskvp.Key;
}
nsman.AddNamespace(nskvp.Key, nskvp.Value);
}
if (!hasDAV)
nsman.AddNamespace(davPrefix , "DAV:");
XPathNodeIterator iterator = navigator.Select("/" + davPrefix + ":" + WebDavXML.PropFind + "/*", nsman);

Related

Reading an XML document with XMLDocument C# 'password' string not returning

I'm trying to read the text in between <keyMaterial></keyMaterial>
I tried using //WLANProfile/MSM/security/sharedKey as the element route, seen in the code below. It refuses to return a value. I have run through the debugger and after the breakpoint at the line: XmlNodeList sharedKeyNodes = wifiProfile.SelectNodes("//WLANProfile/MSM/security/sharedKey");
the SharedKeyNodes object doesn't return a count for. I know it's just a matter of figuring out the element route so I'm not coming here completely hopeless...
System.Xml.XPathNodeList
My XML looks like this:
<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>nosignal</name>
<SSIDConfig>
<SSID>
<hex>6E6F7369676E616C</hex>
<name>nosignal</name>
</SSID>
<nonBroadcast>true</nonBroadcast>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<autoSwitch>false</autoSwitch>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
<FIPSMode xmlns="http://www.microsoft.com/networking/WLAN/profile/v2">false</FIPSMode>
</authEncryption>
<sharedKey>
<keyType>passPhrase</keyType>
<protected>true</protected>
<keyMaterial>01000000D</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>
[ EDIT with the help of LB the new code looks like this and it WORKS! ]
[ For anyone that is struggling with a similar problem. ]
My Class is:
class ProfileManager
{
public static string readProfile() {
XmlDocument wifiProfile = new XmlDocument();
string path = #"C:\temp\nosignal.xml";
string password = "";
wifiProfile.Load(path);
XmlNamespaceManager mgr = new XmlNamespaceManager(wifiProfile.NameTable);
mgr.AddNamespace("ns", "http://www.microsoft.com/networking/WLAN/profile/v1");
XmlNodeList sharedKeyNodes = wifiProfile.SelectNodes("//ns:WLANProfile/ns:MSM/ns:security/ns:sharedKey", mgr);
foreach (XmlNode itemNode in sharedKeyNodes)
{
XmlNode keyMaterialNode = itemNode.SelectSingleNode("ns:keyMaterial", mgr);
if (keyMaterialNode != null)
{
password = keyMaterialNode.InnerText;
}
}
return password;
}
}
I'm close, but still just a bit stuck. Any help would be appreciated!!! Thank you!
You don't use the default XmlNamespace "http://www.microsoft.com/networking/WLAN/profile/v1"
wifiProfile.Load(path);
XmlNamespaceManager mgr = new XmlNamespaceManager(wifiProfile.NameTable);
mgr.AddNamespace("ns", "http://www.microsoft.com/networking/WLAN/profile/v1");
XmlNodeList sharedKeyNodes = wifiProfile.SelectNodes("//ns:WLANProfile/ns:MSM/ns:security/ns:sharedKey",mgr);

How to add namespace to XPath in C#?

I tried to parse an XML file: http://www.ikea.com/pl/pl/catalog/products/30198858?type=xml&dataset=normal,parentCategories,allImages but I obtained error
Namespace Manager or XlstContext needed. This query has a prefix, variable or user-definde function."
Code:
XPathDocument oXPathDocument = new XPathDocument(path);
XPathNavigator oXPathNameNavigator = oXPathDocument.CreateNavigator();
XPathNodeIterator oProductNodesIterator = oXPathNameNavigator.Select(#"/ikea-rest/products/product/items/item");
productModel.productName = oXPathNameNavigator.SelectSingleNode("name").Value;
I found that this error is being caused by lack of namespace so I tried to add it like this:
XmlNamespaceManager nameSpace = new XmlNamespaceManager(oXPathNameNavigator.NameTable);
nameSpace.AddNamespace("ir",path);
Now I have new error:"System.NullReferenceException: Object reference not set to an instance of an object." in line:
productModel.productName = oXPathNameNavigator.SelectSingleNode("name").Value;
What's wrong with my code?
Namespace handling always trips me up, so I had to play around for a bit to get this working right. Your big problem was that when you add a namespace, you provide the address for that namespace, not reuse the path to the xml doc itself.
XPathDocument oXPathDocument = new XPathDocument(path);
XPathNavigator oXPathNameNavigator = oXPathDocument.CreateNavigator();
XmlNamespaceManager nameSpace = new XmlNamespaceManager(oXPathNameNavigator.NameTable);
//use the namespace address provided in the XML, not the path to the xml itself
nameSpace.AddNamespace("ir","http://www.ikea.com/v1.0");
//now you have to scope your query to the namespace you just defined, otherwise xpath will assume the node is not in a namespace
productModel.productName = oXPathNameNavigator.SelectSingleNode("//ir:ikea-rest/products/product/name", nameSpace).Value;
I tested this in LinqPAD and I was able to correctly get at the node you were interested in.

C# & XML - Object reference not set to an instance of an object

Good Evening StackOverFlowers,
I have recently starting coding in XML via Visual Studio 2010. I have come across what seems to be a simple solution, yet the solution escapes me. I get an error for an Object Reference not set, but I don't see what I haven't set. (error here: http://i.imgur.com/CVaxY.png )
My codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
int intDVDID;
XmlDocument myXmlDocument = new XmlDocument();
XmlNode rootNode;
XmlNode selectedDVD;
public void Page_Load(object Src, EventArgs E)
{
intDVDID = Convert.ToInt32(Request.QueryString["id"]);
myXmlDocument.Load(Request.PhysicalApplicationPath + #"dvd.xml");
rootNode = myXmlDocument.DocumentElement;
selectedDVD = rootNode.ChildNodes[intDVDID - 1];
if (!Page.IsPostBack)
{
rootNode.RemoveChild(selectedDVD);
myXmlDocument.Save(Request.PhysicalApplicationPath + #"dvd.xml");
lblMessage.Text = "You have successfully deleted the DVD";
}
}
}
Is it just a matter of saying:
int intDVDID = new intDVDID
I know by reading this you're all going to want to pull your hair out at my inexperience and lack of understanding how to solve this, but I appreciate your time and your patience just looking.
Best regards,
Laura :)
Edit:
Here is my XML:
<?xml version="1.0" encoding="utf-8" ?>
<!-- This XML document describes a DVD library -->
<library>
<DVD id="1">
<title>Breakfast at Tiffany's</title>
<format>Movie</format>
<genre>Classic</genre>
</DVD>
<DVD id="2">
<title>Contact</title>
<format>Movie</format>
<genre>Science fiction</genre>
</DVD>
<DVD id="3">
<title>Little Britain</title>
<format>TV Series</format>
<genre>Comedy</genre>
</DVD>
</library>
It is looking like your selectedDVD may null, put some null checks to verify.
if(selectedDVD != null)
{
}
Edit: In response to your question in comments. Here is some example code. I threw in an xpath even though it seems your case is very simple you may want to use this in the future
string xml = "<xml><node id='1'></node><node id='2'></node></xml>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
//This is an xpath(This replaces your .DocumentElement.ChildNodes[index]
XmlNode desiredNode = xmlDoc.DocumentElement.SelectSingleNode("node[#id='1']");
if (desiredNode != null)
{
xmlDoc.DocumentElement.RemoveChild(desiredNode);
}//if
Pleaase make sure that your query string is not empty it may cause you null reference exeption that you are geting.
if(Request.QueryString["id"]!="")
{
intDVDID = Convert.ToInt32(Request.QueryString["id"]);
}

How do I use XML prefixes in C#?

EDIT: I have now published my app: http://pastebin.com/PYAxaTHU
I was trying to make console-based application that returns my temperature.
using System;
using System.Xml;
namespace GetTemp
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(downloadWebPage(
"http://www.andrewmock.com/uploads/example.xml"
));
XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
man.AddNamespace("aws", "www.aws.com/aws");
XmlNode weather = doc.SelectSingleNode("aws:weather", man);
Console.WriteLine(weather.InnerText);
Console.ReadKey(false);
}
}
}
Here is the sample XML:
<aws:weather xmlns:aws="http://www.aws.com/aws">
<aws:api version="2.0"/>
<aws:WebURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0&stat=BOTHL</aws:WebURL>
<aws:InputLocationURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0</aws:InputLocationURL>
<aws:station requestedID="BOTHL" id="BOTHL" name="Moorlands ES" city="Kenmore" state=" WA" zipcode="98028" country="USA" latitude="47.7383346557617" longitude="-122.230278015137"/>
<aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons/cond024.gif">Mostly Cloudy</aws:current-condition>
<aws:temp units="°F">40.2</aws:temp>
<aws:rain-today units=""">0</aws:rain-today>
<aws:wind-speed units="mph">0</aws:wind-speed>
<aws:wind-direction>WNW</aws:wind-direction>
<aws:gust-speed units="mph">5</aws:gust-speed>
<aws:gust-direction>NW</aws:gust-direction>
</aws:weather>
I'm just not sure how to use XML prefixes correctly here. What is wrong with this?
OK, so based on this XML from the example:
<aws:weather xmlns:aws="http://www.aws.com/aws">
<aws:api version="2.0"/>
<aws:WebURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0&stat=BOTHL</aws:WebURL>
<aws:InputLocationURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0</aws:InputLocationURL>
<aws:station requestedID="BOTHL" id="BOTHL" name="Moorlands ES" city="Kenmore" state=" WA" zipcode="98028" country="USA" latitude="47.7383346557617" longitude="-122.230278015137"/>
<aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons/cond024.gif">Mostly Cloudy</aws:current-condition>
<aws:temp units="°F">40.2</aws:temp>
<aws:rain-today units=""">0</aws:rain-today>
<aws:wind-speed units="mph">0</aws:wind-speed>
<aws:wind-direction>WNW</aws:wind-direction>
<aws:gust-speed units="mph">5</aws:gust-speed>
<aws:gust-direction>NW</aws:gust-direction>
</aws:weather>
You're trying to read out which value?
What's wrong with your code is that your XML namespace is wrong:
You have:
XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
man.AddNamespace("aws", "www.aws.com/aws");
but the XML namespace is: http://www.aws.com/aws
so you should have:
XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
man.AddNamespace("aws", "http://www.aws.com/aws");
So to read out e.g. the temperature, use something like this:
XmlDocument doc = new XmlDocument();
doc.LoadXml(downloadWebPage("http://www.andrewmock.com/uploads/9/1/0/7/9107466/example.xml"));
XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
man.AddNamespace("aws", "http://www.aws.com/aws");
XmlNode temps = doc.SelectSingleNode("/aws:weather/aws:temp", man);
string tempValue = temps.InnerText;
Gives you a value of "40.2" in tempValue
And as Henk Holtermann recommended in his comment - it would be even easier to read this with Linq-to-XML:
XDocument doc = XDocument.Load("http://www.andrewmock.com/uploads/9/1/0/7/9107466/example.xml");
XNamespace aws = "http://www.aws.com/aws";
var weatherNode = doc.Document.Descendants(aws + "weather");
var tempNode = weatherNode.Descendants(aws + "temp").FirstOrDefault();
string tempValue = tempNode.Value;
Of course, this doesn't include any error handling just yet (checking for things like the weatherNode being NULL and stuff like that) - but it gives you an idea.

how to parse a XML in C# using NamspaceManager,XML is having namespaces defined at elements level

i wish to parse following XML
<?xml version="1.0" encoding="UTF-8"?>
<product xmlns="http://products.org">
<make xmlns="http://camera.org">
<model>Camry</model>
</make>
<make xmlns="http://tv.org">
<model>Sony</model>
</make>
</product>
Code written to parse it
This is how i m writing Parsing Code
but in last i m getting null inxmlNode object. Can u tell what more to do .
You can't ignore namespaces in XPath.* The elements in your document all have non-blank namespace URI's.
Your question title indicates you're on the right track: you need to explicitly bind the URI's to prefixes using an XmlNamespaceManager, and use those prefixes in your path expressions.
This program is tested against your input document
using System;
using System.Xml;
public class XPathNamespace
{
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.Load("test1.xml");
XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
xnm.AddNamespace("p", "http://products.org");
xnm.AddNamespace("c", "http://camera.org");
xnm.AddNamespace("t", "http://tv.org");
ShowNode(doc.SelectSingleNode("/p:product", xnm));
ShowNode(doc.SelectSingleNode("/p:product/c:make", xnm));
ShowNode(doc.SelectSingleNode("/p:product/t:make", xnm));
}
private static void ShowNode(XmlNode node) {
Console.WriteLine("<{0}> {1}",
node.LocalName,
node.NamespaceURI);
}
}
and it produces the following output
<product> http://products.org
<make> http://camera.org
<make> http://tv.org
Hope this helps.
(*) This doesn't mean you can't ignore the exact namespace in your XPath. For example, you could match
/*[local-name()='product']
But that's a workaround and illustrates that you still have to deal with the presence of a namespace somehow or other.
This is the best example for you. Please have a look.
http://www.codeproject.com/KB/cpp/myXPath.aspx

Categories

Resources