I'm trying to do something, that may be extremely simple so please bear with me, I just want to get 'DisplayName' from an XML file into a string in my C# code. here's what I have:
THIS IS C#2.0 in VS2005
XML:
<MonitorScope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="System" xmlns="http://tempuri.org/XMLSchema.xsd">
<PersonalSafety>
<MonitorResponseRecord Enabled="false" DisplayName="ValveFailureAtCentralPosition">
<ExpressionMonitor>
<postAlarm>
<AlarmName>Valve_Position_Fault</AlarmName>
<Parameter1> Sensor Position = {X}</Parameter1>
<Parameter2> Sensor Position = {X}</Parameter2>
<Parameter3> Sensor Position = {X}</Parameter3>
</postAlarm>
</ExpressionMonitor>
</MonitorResponseRecord>
<MonitorResponseRecord ... ... ...>
... ...
... ... and so on about 1600 times.
In my C# code I've attempted the following but to No Avail:
C#:
public class AlarmRecord
{
/// <remarks/>
public string PmAlarm;
/// <remarks/>
public string Parameter1;
/// <remarks/>
public string Parameter2;
/// <remarks/>
public string Parameter3;
/// <remarks/>
public string DisplayName;
}
protected void OnPostAlarm(PostAlarm postAlarm)
{
try
{
AlarmRecord alarmRecord = new AlarmRecord();
alarmRecord.PmAlarm = postAlarm.AlarmName;
alarmRecord.Parameter1 = postAlarm.Parameter1;
alarmRecord.Parameter2 = postAlarm.Parameter2;
alarmRecord.Parameter3 = postAlarm.Parameter3;
string fileName = "UMSM.009.8Root.xml";
string fullPath;
fullPath = Path.GetFullPath(fileName);
XmlTextReader reader = new XmlTextReader(new StringReader(fullPath));
System.Xml.XPath.XPathDocument docNav = new System.Xml.XPath.XPathDocument(reader);
System.Xml.XPath.XPathNavigator Q = docNav.CreateNavigator();
System.Xml.XPath.XPathExpression EXE = Q.Compile("MonitorResponseRecord/#DisplayName");
alarmRecord.DisplayName = Convert.ToString(Q.Evaluate(EXE));
alarms.Enqueue( alarmRecord );
}
catch (Exception e)
{
Log.Write(e);
OnUnknownResponse(postAlarm);
}
}
basically my current issue is that durring Debug the issue that I'm noticing is in the line where 'reader' is initialized... the program usually throws an exception here
You could use an XmlReader:
protected void OnPostAlarm(PostAlarm postAlarm)
{
AlarmRecord record = null;
List<AlarmRecord> recordList = new List<AlarmRecord>();
using(XmlReader reader = XmlReader.Create("Xml/bin/UMSM.009.8Root.xml"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "MonitorResponseRecord":
record = new AlarmRecord();
recordList.Add(record);
reader.MoveToAttribute("DisplayName");
record.DisplayName = reader.Value;
break;
case "AlarmName":
record.PmAlarm = reader.ReadString();
break;
case "Parameter1":
record.Parameter1 = reader.ReadString();
break;
case "Parameter2":
record.Parameter2 = reader.ReadString();
break;
case "Parameter3":
record.Parameter3 = reader.ReadString();
break;
}
}
}
}
Best to use XPathNavigator and XPath queries in .NET 2.0.
Reading attributes (like DisplayName in your example) is done by using # in your XPath query.
There's a decent example here. Although the XPathExpression bit isn't necessary, you could just call nav.Evaluate and provide the string XPath query directly.
Example XML (I needed to strip your namespace attributes):
<MonitorScope>
<PersonalSafety>
<MonitorResponseRecord Enabled="false" DisplayName="ValveFailureAtCentralPosition">
<ExpressionMonitor>
<postAlarm>
<AlarmName>Valve_Position_Fault</AlarmName>
<Parameter1> Sensor Position = {X}</Parameter1>
<Parameter2> Sensor Position = {X}</Parameter2>
<Parameter3> Sensor Position = {X}</Parameter3>
</postAlarm>
</ExpressionMonitor>
</MonitorResponseRecord>
</PersonalSafety>
Code example using XPath:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator nodeList = nav.Select("//MonitorScope/PersonalSafety/MonitorResponseRecord");
if (nodeList.Count != 0) {
foreach (XPathNavigator node in nodeList) {
// node queries are relative to MonitorResponseRecord node
string displayName = node.SelectSingleNode("./#DisplayName").Value;
string alarmName = node.SelectSingleNode("ExpressionMonitor/postAlarm/AlarmName").Value;
string param1 = node.SelectSingleNode("ExpressionMonitor/postAlarm/Parameter1").Value;
string param2 = node.SelectSingleNode("ExpressionMonitor/postAlarm/Parameter2").Value;
string param3 = node.SelectSingleNode("ExpressionMonitor/postAlarm/Parameter3").Value;
// do something with values
}
}
Related
So I am trying to validate a xml file against a xsd file using XmlSchemaSet and I tried implementing the following solution in my project and it finds all the errors in the xml file but the line number it gets is always 1 for some reason. Here is the code that deals with the issue:
xmlValidate class:
public class xmlValidate
{
private IList<string> allValidationErrors = new List<string>();
public IList<string> AllValidationErrors
{
get
{
return this.allValidationErrors;
}
}
public void checkForErrors(object sender, ValidationEventArgs error)
{
if (error.Severity == XmlSeverityType.Error || error.Severity == XmlSeverityType.Warning)
{
this.allValidationErrors.Add(String.Format("<br/>" + "Line: {0}: {1}", error.Exception.LineNumber, error.Exception.Message));
}
}
}
Main function:
public string validate(string xmlUrl, string xsdUrl)
{
XmlDocument xml = new XmlDocument();
xml.Load(xmlUrl);
xml.Schemas.Add(null, xsdUrl);
string xmlString = xml.OuterXml;
XmlSchemaSet xmlSchema = new XmlSchemaSet();
xmlSchema.Add(null, xsdUrl);
if (xmlSchema == null)
{
return "No Schema found at the given url.";
}
string errors = "";
xmlValidate handler = new xmlValidate();
XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new ValidationEventHandler(handler.checkForErrors);
settings.Schemas.Add(xmlSchema);
settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
| XmlSchemaValidationFlags.ProcessSchemaLocation
| XmlSchemaValidationFlags.ReportValidationWarnings
| XmlSchemaValidationFlags.ProcessIdentityConstraints;
StringReader sr = new StringReader(xmlString);
using (XmlReader vr = XmlReader.Create(sr, settings))
{
while (vr.Read()) { }
}
if (handler.AllValidationErrors.Count > 0)
{
foreach (String errorMessage in handler.AllValidationErrors)
{
errors += errorMessage;
}
return errors;
}
return "No Errors!";
}
Does anyone see my issue? Thank you in advance!
Could it be, that you load your XML without formatting?
Try with XmlDocument xml = new XmlDocument { PreserveWhitespace = true }
I guess that could be important for getting the right line number but I did not check to be honest.
I'm using this code to save and restore the XML values but I'm in trouble . Rescue usually works the problem and when I try to load the XML . I get this exception that in the image.
line 105 : string text = el.Attribute("Text").Value;
void SaveData() {
XDocument xmlDocument = new XDocument(new XElement("Pages"));
List<XElement> xmlPages = new List<XElement>();
foreach(KeyValuePair<string, string> doc in documents)
xmlDocument.Root.Add(
new XElement("Page",
new XAttribute("nodeName", GetNodeName(doc.Key)),
new XAttribute("pageGuid", doc.Key),
new XAttribute("Rtf", doc.Value)));
xmlDocument.Root.Add(
new XElement("TextEdit",
new XAttribute("Text", textBox1.Text)));
xmlDocument.Save(GetPathToFile());
}
void LoadData() {
try {
XDocument xmlDocument = XDocument.Load(GetPathToFile());
rootNode.Nodes.Clear();
documents.Clear();
foreach(XElement el in xmlDocument.Root.Elements()) {
string nodeName = el.Attribute("nodeName").Value;
string pageGuid = el.Attribute("pageGuid").Value;
string rtf = el.Attribute("Rtf").Value;
string text = el.Attribute("Text").Value;
rootNode.Nodes.Add(new DataNode(nodeName, pageGuid));
documents.Add(pageGuid, rtf);
textBox1.Text = text;
}
} catch(Exception ex) {
MessageBox.Show("No data loaded. Check XML file" + ex.ToString());
}
treeList1.RefreshDataSource();
}
The exception is clear: There is not such attribute el.Attribute("Text"), so you can't try to get it's value. Check for attribute existence before getting it's value.
After research could solve the case.
Solution:
void LoadData() {
try {
XDocument xmlDocument = XDocument.Load(GetPathToFile());
rootNode.Nodes.Clear();
documents.Clear();
foreach(XElement el in xmlDocument.Root.Elements()) {
switch(el.Name.LocalName) {
case "Page":
string nodeName = el.Attribute("nodeName").Value;
string pageGuid = el.Attribute("pageGuid").Value;
string rtf = el.Attribute("Rtf").Value;
rootNode.Nodes.Add(new DataNode(nodeName, pageGuid));
documents.Add(pageGuid, rtf);
break;
case "Text":
textEdit1.Text = el.Attribute("text").Value;
break;
}
}
} catch(Exception ex) {
MessageBox.Show("No data loaded. Check XML file");
}
treeList1.RefreshDataSource();
}
my source like this in C#:
string xml = null;
WebRequest req = WebRequest.Create("https://www.freegeoip.net/xml");
req.Credentials = CredentialCache.DefaultCredentials;
WebResponse res = req.GetResponse();
Stream dataStream = res.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
xml = reader.ReadToEnd();
reader.Close();
res.Close();
am getting response like this :
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<IP>162.158.50.10</IP> //IP address
<CountryCode>IN</CountryCode> //Country Code
<CountryName>India</CountryName>//Country Name
<RegionCode>MH</RegionCode> //Region Code
<RegionName>Maharashtra</RegionName>
<City>Mumbai</City>
<ZipCode></ZipCode>
<TimeZone>Asia/Kolkata</TimeZone>
<Latitude>18.975</Latitude>
<Longitude>72.8258</Longitude>
<MetroCode>0</MetroCode>
</Response>
/// XMl Reponse END///////////////////////////////
I want pass parameters to Database like :
objLogDetails.IPAddress = ???? //Here i want to pass IP :162.158.50.10 from XMl string
HEre are two methods, one using xpath and the other using linq 2 xml:
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
namespace stackexchange
{
class Program
{
private static string theXml = #"<?xml version='1.0' encoding='UTF-8'?>
<Response>
<IP>162.158.50.10</IP> //IP address
<CountryCode>IN</CountryCode> //Country Code
<CountryName>India</CountryName>//Country Name
<RegionCode>MH</RegionCode> //Region Code
<RegionName>Maharashtra</RegionName>
<City>Mumbai</City>
<ZipCode></ZipCode>
<TimeZone>Asia/Kolkata</TimeZone>
<Latitude>18.975</Latitude>
<Longitude>72.8258</Longitude>
<MetroCode>0</MetroCode>
</Response>";
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(theXml);
XmlNode ip = xml.SelectSingleNode("/Response/IP");
Console.Out.WriteLine($"Ip Address: {ip.InnerText}");
XElement root = XElement.Parse(theXml);
XElement address = (from a in root.Descendants() where a.Name == "IP" select a).Single();
Console.Out.WriteLine($"Ip Address: {address.Value}");
}
}
}
What you can do here, is use an XElement to get items from the response and insert into your request.
You can do so like this:
//sets the node or remove
public static void SetOrRemoveNodeValue(XElement root, string xPath, string attributeName, string value)
{
XElement currentNode = root.XPathSelectElement(xPath);
if (currentNode != null)
{
if (currentNode.Attributes().FirstOrDefault(att => att.Name.LocalName.Equals(attributeName)) != null)
{
if (value == string.Empty)
{
currentNode.Attribute(attributeName).Remove();
}
else
{
currentNode.Attribute(attributeName).Value = value;
}
}
then use it like this:
Formatter.SetOrRemoveNodeValue("node", "your value type", "your value");
To extract value from the response, simply use:
currentNode.XPathSelectElement("//Response").Element("Ip").value;
Or simply
currentNode.XPathSelectElement("//Ip").value;
Trying This Code ..
private string mStrXMLStk = Application.StartupPath + "\\Path.xml";
private System.Xml.XmlDocument mXDoc = new XmlDocument();
mXDoc.Load(mStrXMLStk);
XmlNode XNode = mXDoc.SelectSingleNode("/Response");
if (XNode != null)
{
if (XNode != null)
{
int IntChildCount = XNode.ChildNodes.Count;
for (int IntI = 1; IntI <= 1; IntI++)
{
string LocalName = XNode.ChildNodes[IntI].LocalName;
XmlNode Node = mXDoc.SelectSingleNode("/Response/" + LocalName);
string _ip = Node.InnerText;
MessageBox.Show("IP" + _ip);
}
}
}
Completely worked
so I'm a total noob to C# is there any way to make this work?
It would also be a great help if someone could explain why my system doesn't work, and why another version would.
using System;
using System.Xml;
public class XMLManager
{
private XmlTextReader reader;
private XmlDocument document;
private XmlNodeList nodeList;
public void OpenFile(string file)
{
try
{
reader = new XmlTextReader(file);
reader.WhitespaceHandling = WhitespaceHandling.None;
reader.MoveToContent();
document = new XmlDocument();
document.Load(reader);
nodeList = document.SelectNodes(#"Settings/Settings");
}
catch (System.IO.FileNotFoundException)
{
}
}
public void CloseFile()
{
if (reader != null)
{
((IDisposable)reader).Dispose();
reader.Close();
reader = null;
}
document = null;
nodeList = null;
}
public string Get(string attrib)
{
for (int i = 0; i < nodeList.Count; i++)
{
reader.MoveToAttribute(i);
if (reader.Name == attrib)
{
return reader.Value;
}
}
return null;
}
}
Edit: Sorry for my bad formatting, this is my first time posting on Stack Overflow.
You are making multiple mistakes here.
First of all, you don't need a reader to read xml content into an XmlDocument.
Second, while trying to get the attributes, you are trying to proceed to the attributes using the reader which is obviously not having the context of the selected nodes.
Here is your updated XmlManager, but I have to note, there is a logical error too, which is, when the GetAttribute is invoked, you are searching all the Settings/Settings nodes and if find the attribute in any of them, return it. If the xml file contains only one Settings/Settings node, SelectSingleNode is better. I assume the following format:
<Settings>
<Settings attr1="attr1val" attr2="attr2val" />
</Settings>
Note: I also removed CloseFile method because it is no longer required.
public class XMLManager
{
private XmlDocument document;
private XmlNodeList nodeList;
public void OpenFile(string file)
{
document = new XmlDocument();
document.Load(file);
nodeList = document.SelectNodes(#"Settings/Settings");
}
public string Get(string attrib)
{
for (int i = 0; i < nodeList.Count; i++)
{
if (nodeList[i].Attributes[attrib] != null)
{
return nodeList[i].Attributes[attrib].Value;
}
}
return null;
}
}
Overall, you're doing way too much work.
If you have XML in a file, load it directly into an XML (DOM) object using XmlDocument.Load( strFileName );
To iterate all nodes matching an XPath query, see how I run through them.
try {
string strFileName = HttpContext.Current.Server.MapPath("\\data.xml");
XmlDocument xml = new XmlDocument();
xml.Load( strFileName );
foreach (XmlElement ndRow in xml.SelectNodes("//row")) {
string strTemp = ndRow.GetAttribute("foo");
}
} catch (Exception ex) {
Response.Write(ex.Message);
}
I am reading an XML file using XMLDocument and XmlNodeReader.I do not know what happens to the while loop that it fails to run several parts of the code.
Here is my C# code:
public string TitleXml;
public string NameXml;
public string TypeXml;
public string ValueXml;
public Guid GuidXml;
public string DataString;
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(MyParent.xmlstring);
XmlNodeReader xreader = new XmlNodeReader(xdoc);
while (xreader.Read())
{
switch (xreader.Name)
{
case"GUID":
GuidXml = Guid.Parse(xreader.ReadInnerXml());
//after this break the name of the xreader changes.
break;
case "Type":
TypeXml = xreader.ReadInnerXml();
break;
case "Name":
NameXml = xreader.ReadInnerXml();
break;
case "Title":
TitleXml = xreader.ReadInnerXml();
break;
}
}
xreader.Close();
}
Here is my XML:
<Item>
<GUID>9A4FA56F-EAA0-49AF-B7F0-8CA09EA39167</GUID>
<Type>button</Type>
<Title>Save</Title>
<Value>submit</Value>
<Name>btnsave</Name>
<MaxLen>5</MaxLen>
</Item>
It doesn't exactly answer your question, but an (at least according to me) easier way of solving this would be:
XDocument doc = XDocument.Load("test.xml");
string TitleXml = doc.Descendants("Title").Single().Value;
string NameXml = doc.Descendants("Name").Single().Value;
string TypeXml = doc.Descendants("Type").Single().Value;
string ValueXml = doc.Descendants("Value").Single().Value;
Guid GuidXml = Guid.Parse(doc.Descendants("GUID").Single().Value);
I also think you should use Linq-to-XML, but for your example I'd explicitly list the elements, like so (compilable example program):
using System;
using System.Xml.Linq;
namespace ConsoleApplication1
{
internal class Program
{
static void Main()
{
string xml =
#"<Item>
<GUID>9A4FA56F-EAA0-49AF-B7F0-8CA09EA39167</GUID>
<Type>button</Type>
<Title>Save</Title>
<Value>submit</Value>
<Name>btnsave</Name>
<MaxLen>5</MaxLen>
</Item>";
XElement elem = XElement.Parse(xml);
Guid GuidXml = Guid.Parse(elem.Element("GUID").Value);
Console.WriteLine(GuidXml);
string TypeXml = elem.Element("Type").Value;
Console.WriteLine(TypeXml);
string NameXml = elem.Element("Name").Value;
Console.WriteLine(NameXml);
string TitleXml = elem.Element("Title").Value;
Console.WriteLine(TitleXml);
}
}
}