Comparing parsed XML with a string - c#

I'm developing a web service that will receive a XML string and will compare with the internalName string. I'm using LINQ to parse the XML (and I think I'm doing it correctly) but I'm not sure how to compare the "value" withinternalName, per example.
[WebMethod]
public string WMCompare (string xml, string internalName)
{
XDocument xmlDoc = XDocument.Load(xml);
var result = from ele in xmlDoc.Descendants("property")
select new
{
key = (string)ele.Element("key"),
value = (string)ele.Element("value")
};
foreach (var i in result)
{
}
}
}
Thank you for your attention and I'm sorry about the newbie question. It's my first time working with XML.

Considering that you are comparing string with value:
var newResult = result.Where(r => r.value.Equals(internalName))
Alternatively, you may also compare while parsing your XML:
var result1 = from ele in doc.Descendants("property")
where ele.HasElements && ele.Element("value") != null && ele.Element("value").Equals(internalName)
select new
{
key = (string)ele.Element("key"),
value = (string)ele.Element("value")
};

Related

Parsing XML: NullReferenceException for Variable Elements

I'm getting a text string from a website and parsing it into an XDocument. I'm looking to feed the value of certain elements into a very simple object (named NWSevent). My problem is that the original string changes and the XML tree varies; sometimes there are numerous events, up to 40, sometimes there is only one, and sometimes there is only one that does not have all the characteristics. If there are no alerts, then the "event" element has a title, but no areaDesc, summary, or severity.
I have two constructors for NWSevent, one takes in a single string, the other takes in four string arguments. I'm having trouble getting around a NullReferenceException. The if statement below can't do it because there is no value to compare. I'd appreciate any help.
public static void ParseWeatherData(String xmlString)
{
String ticker = string.Empty;
XDocument root = XDocument.Parse(xmlString);
XNamespace ns = XNamespace.Get("http://www.w3.org/2005/Atom");
XNamespace nsCap = XNamespace.Get("urn:oasis:names:tc:emergency:cap:1.1");
//get list of entry elements, set conditions for title, areaDesc, etc
var xlist = root.Descendants(ns + "entry").Select(elem => new
{ //use first or default to deal with possiblity of null return
Title = elem.Descendants(ns + "title").FirstOrDefault(),
AreaDesc = elem.Descendants(nsCap + "areaDesc").FirstOrDefault(),
Severity = elem.Descendants(nsCap + "severity").FirstOrDefault(),
Summary = elem.Descendants(ns + "summary").FirstOrDefault()
});
foreach (var el in xlist) //need to address null values when no alerts
{
if (el.AreaDesc.Value != null) //causes yellow null ERROR; no value exists for el.areaDesc.value
{
String titleIn = el.Title.Value;
String areaIn = el.AreaDesc.Value;
String severityIn = el.Severity.Value;
String summaryIn = el.Summary.Value;
new Models.NWSevent(titleIn, areaIn, severityIn, summaryIn);
}
else
{
String titleIn = el.Title.Value;
new Models.NWSevent(titleIn);
}
}
Embarassing! Props to Dweeberly for pointing it out. I just need to change the if statement from
if (el.AreaDesc.Value != null){}
to if (el.AreaDesc != null){}

get xelement attribute value

I have an XElement that looks like this:
<User ID="11" Name="Juan Diaz" LoginName="DN1\jdiaz" xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/" />
How can I use XML to extract the value of the LoginName attribute? I tried the following, but the q2 "Enumeration yielded no results".
var q2 = from node in el.Descendants("User")
let loginName = node.Attribute(ns + "LoginName")
select new { LoginName = (loginName != null) };
foreach (var node in q2)
{
Console.WriteLine("LoginName={0}", node.LoginName);
}
var xml = #"<User ID=""11""
Name=""Juan Diaz""
LoginName=""DN1\jdiaz""
xmlns=""http://schemas.microsoft.com/sharepoint/soap/directory/"" />";
var user = XElement.Parse(xml);
var login = user.Attribute("LoginName").Value; // "DN1\jdiaz"
XmlDocument doc = new XmlDocument();
doc.Load("myFile.xml"); //load your xml file
XmlNode user = doc.getElementByTagName("User"); //find node by tag name
string login = user.Attributes["LoginName"] != null ? user.Attributes["LoginName"].Value : "unknown login";
The last line of code, where it's setting the string login, the format looks like this...
var variable = condition ? A : B;
It's basically saying that if condition is true, variable equals A, otherwise variable equals B.
from the docs for XAttribute.Value:
If you are getting the value and the attribute might not exist, it is more convenient to use the explicit conversion operators, and assign the attribute to a nullable type such as string or Nullable<T> of Int32. If the attribute does not exist, then the nullable type is set to null.
I ended up using string manipulation to get the value, so I'll post that code, but I would still like to see an XML approach if there is one.
string strEl = el.ToString();
string[] words = strEl.Split(' ');
foreach (string word in words)
{
if (word.StartsWith("LoginName"))
{
strEl = word;
int first = strEl.IndexOf("\"");
int last = strEl.LastIndexOf("\"");
string str2 = strEl.Substring(first + 1, last - first - 1);
//str2 = "dn1\jdiaz"
}
}

Serialize compact XML

I have a class with four fields (DateTime, Enum, string, string). I want to serialize it to and from an XML element or a series of XML elements in a compact manner. For example, I might serialize it to something like this:
<i t='234233' a='3'><u>Username1</u><s1>This is a string</s1></i>
<i t='234233' a='4'><u>Username2</u><s1>This is a string</s1></i>
<i t='223411' a='1'><u>Username3</u><s1>This is a string</s1></i>
Where 'i' is each class instance, 't' is the DateTime ticks, 'a' is the enum value, and the elements are strings.
I'd prefer to not have a root element, but if I do have it, I'd like it to be as small as possible.
I've tried using XmlSerializer with the XmlWriterSettings class but I can't get rid of the namespaces and root element.
What's the best way of doing this? I'm not saving to a file, I'm reading and writing to strings in memory.
System.Xml.Linq
XElement xElem = new XElement("r");
for (int i = 0; i < 3; i++)
{
xElem.Add(
new XElement("i",
new XAttribute("t", "234233"),
new XAttribute("a", "3"),
new XElement("u", "UserName"),
new XElement("s1", "This is a string")
)
);
}
var str = xElem.ToString();
and to read
XElement xElem2 = XElement.Load(new StringReader(str));
foreach(var item in xElem2.Descendants("i"))
{
Console.WriteLine(item.Attribute("t").Value + " " + item.Element("u").Value);
}
PS:
You don't need to convert xElem to string in order to use that xml in memory
If your data is that simple, you can use XmlWriter directly:
class Data {
public DateTime Date { get; set; }
public int Code { get; set; }
public string First { get; set; }
public string Last { get; set; }
}
static void Main() {
var sb = new StringBuilder();
var xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = false;
var elements = new[] {
new Data { Date = DateTime.Now, First = "Hello", Last = "World", Code = 2}
, new Data { Date = DateTime.UtcNow, First = "Quick", Last = "Brown", Code = 4}
};
using (var xw = XmlWriter.Create(sb, xws)) {
xw.WriteStartElement("root");
foreach (var d in elements) {
xw.WriteStartElement("i");
xw.WriteAttributeString("t", ""+d.Date);
xw.WriteAttributeString("a", "" + d.Code);
xw.WriteElementString("u", d.First);
xw.WriteElementString("s1", d.Last);
xw.WriteEndElement();
}
xw.WriteEndElement();
}
Console.WriteLine(sb.ToString());
}
Running this program produces the following output (I added line breaks for clarity; they are not in the output):
<root>
<i t="2/9/2012 3:16:56 PM" a="2"><u>Hello</u><s1>World</s1></i>
<i t="2/9/2012 8:16:56 PM" a="4"><u>Quick</u><s1>Brown</s1></i>
</root>
You need that root element if you would like to read the information back. The most expedient way would be using LINQ2XML:
var xdoc = XDocument.Load(new StringReader(xml));
var back = xdoc.Element("root").Elements("i").Select(
e => new Data {
Date = DateTime.Parse(e.Attribute("t").Value)
, Code = int.Parse(e.Attribute("a").Value)
, First = e.Element("u").Value
, Last = e.Element("s1").Value
}
).ToList();
I'd just use a StringBuilder, personally.
If size is your #1 concern, consider json or yaml instead of XML.
You'll need to implement your own serializer, I believe, which will be a pain. That, or you could manually strip out what you don't need after serializing, which could work.
If size is your concern, you should be serializing to binary using BinaryFormatter. You can always base-64 encode it if you need to store it as a string. (BinaryFormatter works almost just like XmlSerializer, except the output is raw binary, rather than nicely formatted XML.)

How to test whether a node contains particular string or character as its text value?

How to test whether a node contains particular string or character using C# code.
example:
<abc>
<foo>data testing</foo>
<foo>test data</foo>
<bar>data value</bar>
</abc>
Now I need to test the particular node value has the string "testing" ?
The output would be "foo[1]"
You can also that into an XPath document and then use a query:
var xPathDocument = new XPathDocument("myfile.xml");
var query = XPathExpression.Compile(#"/abc/foo[contains(text(),""testing"")]");
var navigator = xpathDocument.CreateNavigator();
var iterator = navigator.Select(query);
while(iterator.MoveNext())
{
Console.WriteLine(iterator.Current.Name);
Console.WriteLine(iterator.Current.Value);
}
This will determine if any elements (not just foo) contain the desired value and will print the element's name and it's entire value. You didn't specify what the exact result should be, but this should get you started. If loading from a file use XElement.Load(filename).
var xml = XElement.Parse(#"<abc>
<foo>data testing</foo>
<foo>test data</foo>
<bar>data value</bar>
</abc>");
// or to load from a file use this
// var xml = XElement.Load("sample.xml");
var query = xml.Elements().Where(e => e.Value.Contains("testing"));
if (query.Any())
{
foreach (var item in query)
{
Console.WriteLine("{0}: {1}", item.Name, item.Value);
}
}
else
{
Console.WriteLine("Value not found!");
}
You can use Linq to Xml
string someXml = #"<abc>
<foo>data testing</foo>
<foo>test data</foo>
</abc>";
XDocument doc = XDocument.Parse(someXml);
bool containTesting = doc
.Descendants("abc")
.Descendants("foo")
.Where(i => i.Value.Contains("testing"))
.Count() >= 1;

How can I transform XML into a List<string> or String[]?

How can I transform the following XML into a List<string> or String[]:
<Ids>
<id>1</id>
<id>2</id>
</Ids>
It sounds like you're more after just parsing rather than full XML serialization/deserialization. If you can use LINQ to XML, this is pretty easy:
using System;
using System.Linq;
using System.Xml.Linq;
public class Test
{
static void Main()
{
string xml = "<Ids><id>1</id><id>2</id></Ids>";
XDocument doc = XDocument.Parse(xml);
var list = doc.Root.Elements("id")
.Select(element => element.Value)
.ToList();
foreach (string value in list)
{
Console.WriteLine(value);
}
}
}
In fact the call to Elements could omit the argument as there are only id elements, but I thought I'd demonstrate how to specify which elements you want.
Likewise I'd normally not bother calling ToList unless I really needed a List<string> - without it, the result is IEnumerable<string> which is fine if you're just iterating over it once. To create an array instead, use ToArray.
Here is a way using XmlDocument :
// A string containing the XML data
string xml = "<Ids><id>1</id><id>2</id></Ids>";
// The list you want to fill
ArrayList list = new ArrayList();
XmlDocument doc = new XmlDocument();
// Loading from a XML string (use Load() for file)
doc.LoadXml(xml);
// Selecting node using XPath syntax
XmlNodeList idNodes = doc.SelectNodes("Ids/id");
// Filling the list
foreach (XmlNode node in idNodes)
list.Add(node.InnerText);
With any type of collection.
For example :
<Ids>
<id>C:\videotest\file0.txt</id>
<id>C:\videotest\file1.txt</id>
</Ids>
class FileFormat
{
public FileFormat(string path)
{
this.path = path;
}
public string FullPath
{
get { return Path.GetFullPath(path); }
}
public string FileName
{
get { return Path.GetFileName(path); }
}
private string path;
}
List<FileFormat> Files =
selectedNode
.Descendants("Ids")
.Elements("Id")
.Select(x => new FileFormat(x.Value))
.Where(s => s.FileName!=string.Empty)
.ToList();
the code to convert a string array to xml
items is a string array
items.Aggregate("", (current, x) => current + ("<item>" + x + "</item>"))
This sample will work with the .NET framework 3.5:
System.Xml.Linq.XElement element = System.Xml.Linq.XElement.Parse("<Ids> <id>1</id> <id>2</id></Ids>");
System.Collections.Generic.List<string> ids = new System.Collections.Generic.List<string>();
foreach (System.Xml.Linq.XElement childElement in element.Descendants("id"))
{
ids.Add(childElement.Value);
}
This sample will work with the .NET framework 4.0:
into a List
List<string> Ids= new List<string>();
Ids= selectedNode.Descendants("Ids").Elements("Id").Select(> x=>x.Value).Where(s =>s!= string.Empty).ToList();
into a string []
string[] Ids= thisNode
.Descendants("Ids") // Ids element
.Elements("Id") // Id elements
.Select(x=>x.Value) // XElement to string
.Where(s =>s!= string.Empty) // must be not empty
.ToArray(); // string to string []
You can use Properties class.
Properties prop = new Properties();
prop.loadFromXML(stream);
Set set = prop.keySet();
You can than access Strings from set for each key. Key is element name of xml.
Here's a way to get typed array from xml by using DataSets. (in this example array of doubles)
DataSet dataSet = new DataSet()
DoubleArray doubles = new DoubleArray(dataSet,0);
dataSet.ReadXml("my.xml");
double a = doubles[0];
public class DoubleArray
{
DataSet dataSet;
int tableIndex;
public DoubleArray(DataSet dataSet,int tableIndex)
{
this.dataSet=dataSet;
this.tableIndex=tableIndex;
}
public double this[int index]
{
get
{
object ret = dataSet.Tables[tableIndex].Rows[index];
if(ret is double?)
return (ret as double?).Value;
else
return double.Parse(ret as string);
}
set
{
object out = dataSet.Tables[tableIndex].Rows[index];
if(out is double?)
dataSet.Tables[tableIndex].Rows[index] = (double?)value;
else
dataSet.Tables[tableIndex].Rows[index] = value.ToString();
}
}
}
Of course parsing doubles and converting them back to strings all the time might be considered as blasphemy by some programmers... Even for me it was hard not to think about such waste of resources... but I guess sometimes it's better to just turn another away.. don't stress it :)

Categories

Resources