i have web service that returns a xmlement like below
<Application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Type>C</Type>
<AppDate>2012-05-01T00:00:00</AppDate>
<Applicants>
<ID>1234</ID>
</Applicants>
<Applicants>
<ID>0</ID>
</Applicants>
<Status>O</Status>
</Application>
on my client side
i have a code like this
XmlElement root = proxy.CallWebservice();
XmlNodeList nodeList;
nodeList = root.SelectNodes("/Application/Applicants");
foreach (XmlNode applicants in nodeList)
{
Console.WriteLine(applicants.InnerXml);
}
PROBLEM: i cannot get anything to output.
BUT
if i do this
File.WriteAllText(#"d:\output.xml", root.OuterXml.ToString());
XmlDocument doc = new XmlDocument();
doc.Load(#"d:\output.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList nodeList;
nodeList = root.SelectNodes("/Application/Applicants");
foreach (XmlNode title in nodeList)
{
Console.WriteLine(title.InnerXml);
}
IT WORKS, i do not want to have to write to the file to just read the nodes. what am i doing wrong?
i had to use
XmlNodeList nodeList;
nodeList = root.SelectNodes("Applicants");
as my root was application
Related
Xml code:
<Report>
<ChartData>
<ListName>area</ListName>
<ViewName>Selecte List</ViewName>
<YAxisFields>
<YAxisField>
<Name>Scheduled Start Date/Time</Name>
<DataType>DateTime</DataType>
<Category>Year</Category>
</YAxisField>
</YAxisFields>
<XAxisFields>
<XAxisField>
<Name>Release Type</Name>
<DataType>String</DataType>
<Category>
</Category>
</XAxisField>
</XAxisFields>
</ChartConfig>
</Report>
I got the value for the subnode listname and viewname by using the
below code,
XmlDocument doc = new XmlDocument();
doc.Load("XmlFileName");
XmlNodeList node = doc.SelectNodes("Report/ChartData");
foreach (XmlNode xn in node)
{ xn["ListName"].InnerXml = chartname;
xn["ViewName"].InnerXml = SelectedList;
**xn["YAxisFields/YAxisField"].InnerXml = yaxisfield; //not working, need to get the value for this xml node,need help in this line dono how to proceed**
doc.Save("XmlFilename");
}
First i have tried with code like this instead of above code,in this
i need to create number of objects in order get the value for each
node so i tried by creating object for xmlnodelist then i used
foreach loop to get the value for each node but in this couldnt get
the value for YAxisFields/YAxisField because it also has parent node
as YAxisFields and subnode as YAxisField so there is only way to
create number of objects for xmlnode or is there any other way to do
this?
XmlDocument doc = new XmlDocument();
doc.Load("XmlFileName");
XmlNode Listnode = doc.SelectSingleNode("Report/ChartData/ListName");
XmlNode Viewnode = doc.SelectSingleNode("Report/ChartData/ViewName");
if (Listnode != null)
{
Listnode.InnerXml = chartname;
Viewnode.InnerXml = SelectedList; ;
doc.Save("XmlFileName");
Use Linq to XML XDocument, like this:
doc.Root.Descendants("ChartData").ToList().ForEach(node =>
{
node.Element("ListName").Value = chartname;
node.Element("ViewName").Value = SelectedList;
});
Can somebody explain me please why this is not working?
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Bing2_ss.xml");
//Instantiate an XmlNamespaceManager object.
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable);
//Add the namespaces used in feed to the XmlNamespaceManager.
//xmlnsManager.AddNamespace("base", "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Composite");
xmlnsManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
xmlnsManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices/");
xmlnsManager.AddNamespace(String.Empty, "http://www.w3.org/2005/Atom");
XmlNodeList nodeList;
XmlElement root = xmlDoc.DocumentElement;
nodeList = root.SelectNodes("//m:properties/d:WebTotal", xmlnsManager);
foreach (XmlNode nd in nodeList)
{
System.Console.WriteLine(nd.InnerText);
}
This is the original xml: http://gezond-afslanken.info/bing2_ss.xml
You have an extra trailing slash on one of your namespaces. Try
xmlnsManager.AddNamespace("d","http://schemas.microsoft.com/ado/2007/08/dataservices");
I have an getting an xml response on this format
<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n
<PlatformResponse xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://platform.intuit.com/api/v1\">\r\n
<ErrorMessage>OAuth Token rejected</ErrorMessage>\r\n
<ErrorCode>270</ErrorCode>\r\n
<ServerTime>2012-06-19T00:01:31.5150146Z</ServerTime>\r\n
</PlatformResponse>
I need to grab the value in the <ErrorCode> node, for that I did the following but it is not getting any values..
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlResponse);
XmlNodeList xnList = xml.SelectNodes("PlatformResponse");
foreach (XmlNode xn in xnList)
{
result.Message = xn["ErrorCode"].InnerText;
}
Any help would be much appreciated.
There seems to be some dirt in your PlatformResponse node that's giving problems, ( xmlns:xsd= etc... )
Using this xml
String sXml = #"<?xml version='1.0' encoding='utf-8'?>
<PlatformResponse >
<ErrorMessage>OAuth Token rejected</ErrorMessage>
<ErrorCode>270</ErrorCode>
<ServerTime>2012-06-19T00:01:31.5150146Z</ServerTime>
</PlatformResponse>";
And select like
XmlNodeList xnList = xml.SelectNodes("/PlatformResponse");
Your code works fine.
I just tested the code and it does work fine:
XmlDocument xml = new XmlDocument();
XmlTextReader reader = new XmlTextReader("Path_to_your_xml");
xml.Load(reader);
XmlNodeList xnList = xml.SelectNodes("PlatformResponse");
foreach (XmlNode xn in xnList)
{
MessageBox.Show(xn["ErrorCode"].InnerText);
}
For this, since the attribute is on the main document element itself, you can simply do
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlText);
result.Message = xml.DocumentElement["ErrorCode"].InnerText
I have this XML at http://localhost/file.xml:
<?xml version="1.0" encoding="utf-8"?>
<val:Root xmlns:val="http://www.hw-group.com/XMLSchema/ste/values.xsd">
<Agent>
<Version>2.0.3</Version>
<XmlVer>1.01</XmlVer>
<DeviceName>HWg-STE</DeviceName>
<Model>33</Model>
<vendor_id>0</vendor_id>
<MAC>00:0A:DA:01:DA:DA</MAC>
<IP>192.168.1.1</IP>
<MASK>255.255.255.0</MASK>
<sys_name>HWg-STE</sys_name>
<sys_location/>
<sys_contact>
HWg-STE:For more information try http://www.hw-group.com
</sys_contact>
</Agent>
<SenSet>
<Entry>
<ID>215</ID>
<Name>Home</Name>
<Units>C</Units>
<Value>27.7</Value>
<Min>10.0</Min>
<Max>40.0</Max>
<Hyst>0.0</Hyst>
<EmailSMS>1</EmailSMS>
<State>1</State>
</Entry>
</SenSet>
</val:Root>
I am trying to read this from my c# code:
static void Main(string[] args)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load("http://localhost/file.xml");
XmlElement root = xmlDoc.DocumentElement;
// Create an XmlNamespaceManager to resolve the default namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("val", "http://www.hw-group.com/XMLSchema/ste/values.xsd");
XmlNodeList nodes = root.SelectNodes("/val:SenSet/val:Entry");
foreach (XmlNode node in nodes)
{
string name = node["Name"].InnerText;
string value = node["Value"].InnerText;
Console.Write("name\t{0}\value\t{1}", name, value);
}
Console.ReadKey();
}
}
Problem is that the node is empty. I understand this is a common newbie problem when reading XML, still not able to solve what I am doing wrong, probably something with the Namespace "val" ?
You need to pass the namespace manager into the SelectNodes()
method.
Edit: corrected code
XmlNodeList nodes = root.SelectNodes("/val:Root/SenSet/Entry", nsmgr);
Just change you Xpath to:
XmlNodeList nodes1 = root.SelectNodes("/val:Root/SenSet/Entry",nsmgr);
Or:
XmlNodeList nodes = root.SelectNodes("SenSet/Entry");
Your xpath query string should be:
XmlNodeList nodes = root.SelectNodes("/val:Root/SenSet/Entry", nsmgr);
or more concisely,
XmlNodeList nodes = root.SelectNodes("//SenSet/Entry", nsmgr);
A pretty newbie question as I rarely work with XML. I'm trying out writing stuff for the Subsonic API. The xml looks like this
<?xml version="1.0" encoding="UTF-8"?>
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.6.0">
<indexes lastModified="1313158157783">
<index name="A">
<artist name="Albums" id="5c5c3139322e3136382e322e31305c566f6c756d655f315c4d757369635c416c62756d73"/>
</index>
<index name="S">
<artist name="Singles" id="5c5c3139322e3136382e322e31305c566f6c756d655f315c4d757369635c53696e676c6573"/>
</index>
</indexes>
</subsonic-response>
I'm just trying to get the index nodes.
I'm trying this, but not sure if I am doing this right. Both the SelectNodes and SelectSingleNode are returning emtpy. I'm sure I am missing something simple.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(index.NameTable);
nsmgr.AddNamespace("", "http://subsonic.org/restapi");
XmlNodeList xnList = index.SelectNodes("/subsonic-response/indexes/index", nsmgr);
XmlNode mainnode = index.SelectSingleNode("/subsonic-response", nsmgr);
foreach (XmlNode xn in xnList)
{
}
I've tried with and without the namespacemanager and it's the same thing
Try using a non-empty XML namespace prefix:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(index.NameTable);
nsmgr.AddNamespace("x", "http://subsonic.org/restapi");
XmlNodeList xnList = index.SelectNodes("/x:subsonic-response/x:indexes/x:index", nsmgr);
XmlNode mainnode = index.SelectSingleNode("/x:subsonic-response", nsmgr);
I've had trouble with trying to use the empty string as a (default) XML namespace prefix
How about:
nsmgr.AddNamespace("x", "http://subsonic.org/restapi");
And:
XmlNodeList xnList = index.SelectNodes("/x:subsonic-response/x:indexes/x:index", nsmgr);