XDocument containing namespaces

Try this, works for me XNamespace nsSys = “http://schemas.microsoft.com/2004/06/windows/eventlog/system”; XElement xEl2 = xDoc.Element(nsSys + “System”); XElement xEl3 = xEl2.Element(nsSys + “Correlation”); XAttribute xAtt1 = xEl3.Attribute(“ActivityID”); String sValue = xAtt1.Value; You need to use Namespaces. Full source for trial public static void Main() { XElement xDoc = XElement.Parse( @”<E2ETraceEvent xmlns=””http://schemas.microsoft.com/2004/06/E2ETraceEvent””> <System xmlns=””http://schemas.microsoft.com/2004/06/windows/eventlog/system””> <EventID>589828</EventID> <Type>3</Type> <SubType Name=””Information””>0</SubType> … Read more

linq question: querying nested collections

To find an answer. questions.SelectMany(q => q.Answers).Where(a => a.Name == “SomeName”) To find the question of an answer. questions.Where(q => q.Answers.Any(a => a.Name == “SomeName”)) In fact you will get collections of answers or questions and you will have to use First(), FirstOrDefault(), Single(), or SingleOrDefault() depending on your needs to get one specific answer … Read more

C# Distinct on IEnumerable with custom IEqualityComparer

The problem is with your GetHashCode. You should alter it to return the hash code of AllianceName instead. int IEqualityComparer<Village>.GetHashCode(Village obj) { return obj.AllianceName.GetHashCode(); } The thing is, if Equals returns true, the objects should have the same hash code which is not the case for different Village objects with same AllianceName. Since Distinct works … Read more

XDocument: saving XML to file without BOM

Use an XmlTextWriter and pass that to the XDocument’s Save() method, that way you can have more control over the type of encoding used: var doc = new XDocument( new XDeclaration(“1.0”, “utf-8”, null), new XElement(“root”, new XAttribute(“note”, “boogers”)) ); using (var writer = new XmlTextWriter(“.\\boogers.xml”, new UTF8Encoding(false))) { doc.Save(writer); } The UTF8Encoding class constructor has … Read more

Uses for the ‘"’ entity in HTML

It is impossible, and unnecessary, to know the motivation for using &quot; in element content, but possible motives include: misunderstanding of HTML rules; use of software that generates such code (probably because its author thought it was “safer”); and misunderstanding of the meaning of &quot;: many people seem to think it produces “smart quotes” (they … Read more

Finding element in XDocument?

Elements() will only check direct children – which in the first case is the root element, in the second case children of the root element, hence you get a match in the second case. If you just want any matching descendant use Descendants() instead: var query = from c in xmlFile.Descendants(“Band”) select c; Also I … Read more

How to change XML Attribute

Mike; Everytime I need to modify an XML document I work it this way: //Here is the variable with which you assign a new value to the attribute string newValue = string.Empty; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFile); XmlNode node = xmlDoc.SelectSingleNode(“Root/Node/Element”); node.Attributes[0].Value = newValue; xmlDoc.Save(xmlFile); //xmlFile is the path of your file to be … Read more