public static void Start() { var xml = new SampleXml().ReturnSampleXml(); XDocument doc = XDocument.Parse(xml); IEnumerable <string> personNames = from p in doc.Descendants("person") where p.Descendants("phonenumber").Any() let name = (string)p.Attribute("firstname") + " " + (string)p.Attribute("lastname") orderby name select name; }
public static void Start() { var xml = new SampleXml().ReturnSampleXml(); XDocument doc = XDocument.Parse(xml); IEnumerable <string> personNames = from p in doc.Descendants("person") select(string) p.Attribute("firstname") + " " + (string)p.Attribute("lastname"); foreach (string s in personNames) { Console.WriteLine(s); } // Displays: // John Doe // Jane Doe }
public static void Start() { var xml = new SampleXml().ReturnSampleXml(); XElement root = XElement.Parse(xml); XElement newTree = new XElement("people", from p in root.Descendants("person") let name = (string)p.Attribute("firstname") + (string)p.Attribute("lastname") let contactDetails = p.Element("contactdetails") select new XElement("person", new XAttribute("IsMale", name.Contains("john")), p.Attributes(), new XElement("contactdetails", contactDetails.Element("emailaddress"), contactDetails.Element("phonenumber") ?? new XElement("phonenumber", "112233455")))); }
public static void Start() { var xml = new SampleXml().ReturnSampleXml(); XElement root = XElement.Parse(xml); foreach (XElement p in root.Descendants("person")) { string name = (string)p.Attribute("firstname") + (string)p.Attribute("lastname"); p.Add(new XAttribute("IsMale", name.Contains("john"))); XElement contactDetails = p.Element("contactdetails"); if (!contactDetails.Descendants("phonenumber").Any()) { contactDetails.Add(new XElement("phonenumber", "001122334455")); } } }
/// <summary> /// Creates a new instance of Sample /// </summary> /// <param name="labReportXml">Must be a EN:LabReport XML element from the XMLSampling schema</param> /// <param name="sampleNamespace">The value of the EN namespace as defined in the XMLSampling schema</param> /// <param name="labReportXml">The EN:LabReport element of which this sample is a part. This will be used for reconstructing the XML file later.</param> public Sample(XElement labReportXml, XNamespace sampleNamespace, string filename) { initSample(); XmlNamespace = sampleNamespace; this.XmlFilename = filename; this.XmlParseDate = DateTime.Now; this.LabIdentificationXml = (from sam in labReportXml.Descendants(sampleNamespace + "LabIdentification") select sam).SingleOrDefault(); LabIdentificationXmlString = LabIdentificationXml.ToString(); this.SampleXml = (from sam in labReportXml.Descendants(sampleNamespace + "Sample") select sam).SingleOrDefault(); SampleXmlString = SampleXml.ToString(); var element = (from sam in SampleXml.Descendants(sampleNamespace + "LabSampleIdentifier") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.LabSampleIdentifier = element.Value; } element = (from sam in LabIdentificationXml.Descendants(sampleNamespace + "LabAccreditationIdentifier") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.LabAccredidationIdentifier = element.Value; } element = (from sam in SampleXml.Descendants(sampleNamespace + "PWSIdentifier") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.PWSIdentifier = element.Value; } element = (from sam in SampleXml.Descendants(sampleNamespace + "PWSFacilityIdentifier") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.PWSFacilityIdentifier = element.Value; } element = (from sam in SampleXml.Descendants(sampleNamespace + "SampleRuleCode") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.SampleRuleCode = element.Value; } element = (from sam in SampleXml.Descendants(sampleNamespace + "SampleMonitoringTypeCode") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.SampleMonitoringTypeCode = element.Value; } element = (from sam in SampleXml.Descendants(sampleNamespace + "SampleCollectionEndDate") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.SampleCollectionEndDate = Convert.ToDateTime(element.Value); _hasCollectionDate = true; } element = (from sam in SampleXml.Descendants(sampleNamespace + "SampleCollectionEndTime") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value) && this.SampleCollectionEndDate != new DateTime(0)) { this.SampleCollectionEndDate = ((DateTime)this.SampleCollectionEndDate).Add(TimeSpan.Parse(element.Value)); DateTime testForZeroTime = DateTime.MinValue; testForZeroTime = testForZeroTime.Add(TimeSpan.Parse(element.Value)); if (testForZeroTime.Equals(DateTime.MinValue)) { _hasCollectionTime = false; } else { _hasCollectionTime = true; } } element = (from sam in SampleXml.Descendants(sampleNamespace + "SampleVolume") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.SampleVolume = Convert.ToDouble(element.Value); } element = (from sam in SampleXml.Descendants(sampleNamespace + "SampleLocationIdentifier") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.SampleLocationIdentifier = element.Value; } element = (from sam in SampleXml.Descendants(sampleNamespace + "SampleLocationCollectionAddress") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.SampleLocationCollectionAddress = element.Value; } element = (from sam in SampleXml.Descendants(sampleNamespace + "SampleCollector") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.SampleCollector = element.Value; _hasCollectorName = true; } element = (from sam in SampleXml.Descendants(sampleNamespace + "SampleLaboratoryReceiptDate") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.SampleLaboratoryReceiptDate = Convert.ToDateTime(element.Value); _hasLabReceiptDate = true; } element = (from sam in SampleXml.Descendants(sampleNamespace + "LabSampleCompositeDate") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { this.LabSampleCompositeDate = Convert.ToDateTime(element.Value); _isCompositeSample = true; } element = (from sam in SampleXml.Descendants(sampleNamespace + "Comments") select sam).SingleOrDefault(); if (element != null && !string.IsNullOrWhiteSpace(element.Value)) { string comments = element.Value; if (comments.Contains("Reporting Lab")) { string labId = comments.Substring(comments.LastIndexOf(" ") + 1); this.SubmittingLabIdentifier = labId; } } HandleFedTypeAndMonitoringType(); }
public static void Start() { var sampleXml = new SampleXml().ReturnSampleXml(); Console.WriteLine(sampleXml); }