public static XElement ElementThrow(this XContainer container, string name) { var node = container.Element(name); if (node == null) { throw new Exception("Can not find " + name); } return(node); }
private static IEnumerable <Uri> GetEndpoints(XContainer storageServicePropertiesElement, XNamespace azureNamespace, string endpointElement) { var endpointsElement = storageServicePropertiesElement.Element(azureNamespace + endpointElement); return(endpointsElement != null ? endpointsElement.Elements() .Select(e => new Uri(e.Value)) .ToList() : Enumerable.Empty <Uri>()); }
public static string GetElementValue(this XContainer parentElement, string elementName) { XElement element = parentElement.Element(elementName); if (element == null) { return(null); } return(element.Value); }
private ContractFeedEvent ExtractContractFeedEvent(XContainer element) { try { var id = element.Element(_nsAtom + "id")?.Value.Split(':').ElementAt(1); _logger.Info($"Bookmark Id: {id}"); var contract = element .Element(_nsAtom + "content")? .Descendants(_nsUrn + "contract")?.First() .Element(_nsUrn + "contracts")?.Descendants(_nsUrn + "contract").First(); if (contract == null) { return(null); } var hierarchyType = contract.Element(_nsUrn + "hierarchyType")?.Value; var fundingTypeCode = contract.Element(_nsUrn + "fundingType")?.Element(_nsUrn + "fundingTypeCode")?.Value; var status = contract.Element(_nsUrn + "contractStatus")?.Element(_nsUrn + "status")?.Value; var parentStatus = contract.Element(_nsUrn + "contractStatus")?.Element(_nsUrn + "parentStatus")?.Value; var updatedString = element.Element(_nsAtom + "updated")?.Value; var updated = DateTime.Parse(updatedString); var ukprn = contract.Element(_nsUrn + "contractor")?.Element(_nsUrn + "ukprn")?.Value; return(new ContractFeedEvent { Id = new Guid(id), ProviderId = long.Parse(ukprn), HierarchyType = hierarchyType, FundingTypeCode = fundingTypeCode, Status = status, ParentStatus = parentStatus, Updated = updated }); } catch (Exception ex) { _logger.Warn(ex, "Problem extracting contract feed event"); return(null); } }
public static string ElementValue(this XContainer container, XName name) { if (container == null) { return(null); } var element = container.Element(name); return(element != null ? element.Value : null); }
public MigrationData(XContainer migrationXMLData) { if (migrationXMLData == null) { return; } UncensorGUID = migrationXMLData.Element("guidUncensor")?.Value; BodyGUID = migrationXMLData.Element("guidBody")?.Value; PenisGUID = migrationXMLData.Element("guidPenis")?.Value; BallsGUID = migrationXMLData.Element("guidBalls")?.Value; //These things can be null if the XML doesn't exist or empty strings if it does exist but is left blank //Set everything to null for easier checks UncensorGUID = UncensorGUID.IsNullOrWhiteSpace() ? null : UncensorGUID; BodyGUID = BodyGUID.IsNullOrWhiteSpace() ? null : BodyGUID; PenisGUID = PenisGUID.IsNullOrWhiteSpace() ? null : PenisGUID; BallsGUID = BallsGUID.IsNullOrWhiteSpace() ? null : BallsGUID; }
public static string ElementValueOrDefault(this XContainer container, string name, string @default) { var node = container.Element(name); if (node == null) { return(@default); } return(node.Value); }
private static ICollection <DefaultCategory> LoadCategories(XContainer container) { var result = new List <DefaultCategory>(); var categories = container.Element("categories"); TraverseCategories(result, categories, "expenses", CategoryType.Expense); TraverseCategories(result, categories, "incomes", CategoryType.Income); return(result); }
private static XElement ElementVerified(this XContainer x, XName name) { var xElement = x.Element(name); if (xElement == null) { throw new XmlException(String.Format("Could not find element \"{0}\"", name)); } return(xElement); }
private void ReadLineStudies(XContainer xRoot) { XElement xmlLineStudies = xRoot.Element("LineStudies"); if (xmlLineStudies == null) { return; } int useablePanelsCount = UseablePanelsCount; foreach (XElement node in xmlLineStudies.Elements()) { if (node.Name != "LineStudy") { return; } int panelIndex = ConvertFromXmlEx("PanelIndex", node, -1); if (panelIndex == -1) { continue; } if (panelIndex >= useablePanelsCount) { panelIndex = 0; } string key = ConvertFromXmlEx("Key", node, String.Empty); string studyTypeS = ConvertFromXmlEx("StudyType", node, String.Empty); LineStudy.StudyTypeEnum studyType = (LineStudy.StudyTypeEnum)System.Enum.Parse(typeof(LineStudy.StudyTypeEnum), studyTypeS, true); Brush stroke = (Brush)ConvertFromXml("StrokeColor", node); object[] extrArgs = (object[])ConvertFromBinary(ConvertFromXmlEx("ExtraArgs", node, string.Empty), typeof(object[])); LineStudy lineStudy = CreateLineStudy(studyType, key, stroke, panelIndex); lineStudy.SetArgs(extrArgs); double x1Value = ConvertFromXmlEx("X1Value", node, 0.0); double x2Value = ConvertFromXmlEx("X2Value", node, 0.0); double y1Value = ConvertFromXmlEx("Y1Value", node, 0.0); double y2Value = ConvertFromXmlEx("Y2Value", node, 0.0); lineStudy.SetXYValues(x1Value, y1Value, x2Value, y2Value); lineStudy._selectable = ConvertFromXmlEx("Selectable", node, false); lineStudy.StrokeThickness = ConvertFromXmlEx("StrokeThickness", node, 1.0); lineStudy.StrokeType = (LinePattern)System.Enum.Parse(typeof(LinePattern), ConvertFromXmlEx("StrokeType", node, LinePattern.Solid.ToString()), true); lineStudy.Opacity = ConvertFromXmlEx("Opacity", node, 1.0); if (lineStudy is IShapeAble) { IShapeAble shapeAble = (IShapeAble)lineStudy; shapeAble.Fill = (Brush)ConvertFromXml("Fill", node); } } }
private void ReadWorkSpace(XContainer xRoot) { XElement xmlWorkspace = xRoot.Element("WorkSpace"); if (xmlWorkspace == null) { return; } try { ShowAnimations = ConvertFromXmlEx("ShowAnimations", xmlWorkspace, false); ScaleAlignment = (ScaleAlignmentTypeEnum)System.Enum.Parse(typeof(ScaleAlignmentTypeEnum), ConvertFromXmlEx("YAxes", xmlWorkspace, String.Empty), true); ScalingType = (ScalingTypeEnum)System.Enum.Parse(typeof(ScalingTypeEnum), ConvertFromXmlEx("ScalingType", xmlWorkspace, String.Empty), true); YGrid = ConvertFromXmlEx("ShowYGrid", xmlWorkspace, true); XGrid = ConvertFromXmlEx("ShowXGrid", xmlWorkspace, true); GridStroke = (Brush)ConvertFromXml("GridStroke", xmlWorkspace); ThreeDStyle = ConvertFromXmlEx("ThreeDStyle", xmlWorkspace, true); UpColor = Utils.StringToColor(ConvertFromXmlEx("UpColor", xmlWorkspace, String.Empty)); DownColor = Utils.StringToColor(ConvertFromXmlEx("DownColor", xmlWorkspace, String.Empty)); CrossHairs = ConvertFromXmlEx("CrossHairs", xmlWorkspace, false); LeftChartSpace = ConvertFromXmlEx("LeftChartSpace", xmlWorkspace, 5); RightChartSpace = ConvertFromXmlEx("RightChartSpace", xmlWorkspace, 10); RealTimeXLabels = ConvertFromXmlEx("RealTimeData", xmlWorkspace, true); FontFace = ConvertFromXmlEx("FontFace", xmlWorkspace, String.Empty); FontForeground = (Brush)ConvertFromXml("FontForeground", xmlWorkspace); PriceStyle = (PriceStyleEnum)System.Enum.Parse(typeof(PriceStyleEnum), ConvertFromXmlEx("PriceStyle", xmlWorkspace, String.Empty), true); DisplayTitles = ConvertFromXmlEx("DisplayTitles", xmlWorkspace, true); DarvasBoxes = ConvertFromXmlEx("DarvasBoxes", xmlWorkspace, false); ChartType = (ChartTypeEnum)System.Enum.Parse(typeof(ChartTypeEnum), ConvertFromXmlEx("ChartType", xmlWorkspace, String.Empty), true); TickPeriodicity = ConvertFromXmlEx("TickPeriodicity", xmlWorkspace, 5); TickCompressionType = (TickCompressionEnum)System.Enum.Parse(typeof(TickCompressionEnum), ConvertFromXmlEx("TickCompressionType", xmlWorkspace, String.Empty), true); InfoPanelFontSize = ConvertFromXmlEx("InfoPanelFontSize", xmlWorkspace, 10); InfoPanelFontFamily = new FontFamily(ConvertFromXmlEx("InfoPanelFontFamily", xmlWorkspace, "Arial")); InfoPanelLabelsForeground = (Brush)ConvertFromXml("InfoPanelLabelsForeground", xmlWorkspace); InfoPanelLabelsBackground = (Brush)ConvertFromXml("InfoPanelLabelsBackground", xmlWorkspace); InfoPanelValuesForeground = (Brush)ConvertFromXml("InfoPanelValuesForeground", xmlWorkspace); InfoPanelValuesBackground = (Brush)ConvertFromXml("InfoPanelValuesBackground", xmlWorkspace); InfoPanelPosition = (InfoPanelPositionEnum)System.Enum.Parse(typeof(InfoPanelPositionEnum), ConvertFromXmlEx("InfoPanelPosition", xmlWorkspace, String.Empty), true); IndicatorDialogBackground = (Brush)ConvertFromXml("IndicatorDialogBackground", xmlWorkspace); if (_panelsContainer._infoPanel != null) { _panelsContainer._infoPanel.Position = PointEx.Parse(ConvertFromXmlEx("InfoPanelPositionCoords", xmlWorkspace, "0,0")); } for (int i = 0; i < _priceStyleParams.Length; i++) { _priceStyleParams[i] = ConvertFromXmlEx("PriceStyleParam_" + i, xmlWorkspace, 0.0); } } catch (Exception ex) { Debug.WriteLine(ex); } }
public static XElement GetOrAddElement(this XContainer parent, XName name) { var element = parent.Element(name); if (element == null) { element = new XElement(name); parent.Add(element); } return(element); }
private static Decimal GetDecimalFromXml(XContainer container, String name, Decimal defaultValue = 0) { var item = container.Element(name); if (item != null) { Decimal.TryParse(item.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out defaultValue); } return(defaultValue); }
/// <remarks> /// Source: http://stackoverflow.com/a/14892813/1636276 /// </remarks> public static XElement GetOrCreateElement(this XContainer container, string name) { var element = container.Element(name); if (element == null) { element = new XElement(name); container.Add(element); } return(element); }
public static XElement Element(this XContainer node, XName name, bool ignoreNamespace) { if (ignoreNamespace) { return(node.Elements().FirstOrDefault(e => e.Name.LocalName == name.LocalName)); } else { return(node.Element(name)); } }
/// <summary> /// Finds the element text. /// </summary> /// <param name="parent">The parent.</param> /// <param name="element">The element.</param> /// <returns>The element value or null if not found.</returns> protected static string FindElementText(XContainer parent, XName element) { if (parent == null) { throw new ArgumentNullException("parent"); } var child = parent.Element(element); return(child == null ? null : child.Value); }
private static XElement EnsureElement(XContainer container, string elementName) { var element = container.Element(elementName); if (element == null) { throw new InvalidOperationException($"Invalid manifest format. Missing '{elementName}' element name"); } return(element); }
public static XElement ShouldHaveElement(this XContainer parent, string name) { var elem = parent.Element(xmlNamespace + name); if (elem == null) { throw new AssertionException(string.Format("Could not find an element named {0}", name)); } return(elem); }
private void LoadThreeStateItemContainerOrdered <T>(XContainer xParent, string elementName, string entryName, ThreeStateItemContainerOrdered <T> container) where T : Def { var xFoundElement = xParent.Element(elementName); if (xFoundElement == null) { return; } container.FilterBooleanState = LoadEnum <FilterBoolean>(xFoundElement, "FilterBooleanState"); LoadBoolean(xFoundElement, "OffPartialNoSelect", b => container.OffPartialNoSelect = b); LoadBoolean(xFoundElement, "OrderedFiltering", b => container.OrderedFiltering = b); var orderedList = new List <T>(); foreach (var xElement in xFoundElement.Elements(entryName)) { var xDefName = xElement.Element(DefNameNode); if (xDefName == null) { goto EnsureAllEntriesPresent; } var def = LoadDef <T>(xElement, DefNameNode) as T; if (def == null) { goto EnsureAllEntriesPresent; } orderedList.Add(def); if (!container.TryGetValue(def, out var threeStateItem)) { goto EnsureAllEntriesPresent; } var state = LoadEnum <MultiCheckboxState>(xElement, StateNode); threeStateItem.State = state; } EnsureAllEntriesPresent: foreach (var entry in container) { if (orderedList.Contains(entry.Key)) { continue; } orderedList.Add(entry.Key); } container.SetNewOrder(orderedList); }
private static string GetWordIpa(XContainer ipaDocument, string word) { var wordIpa = ipaDocument.Element("IPADict") ?.Element("WordList") ?.Elements("IpaEntry") .Where(d => d.Element("Item")?.Value.ToString() == word) .Select(d => d.Element("Ipa")?.Value.ToString()) .DefaultIfEmpty(String.Empty).FirstOrDefault(); return(wordIpa); }
private static TrackManiaCallback?TryParseCallback(XContainer messageElement) { XElement methodNameElement = messageElement.Element("methodName"); if (methodNameElement == null) { return(null); } return(TryParseCallback(methodNameElement.Value.Trim())); }
private static void loadCategories(Post post, XContainer doc) { var categories = doc.Element("categories"); if (categories == null) { return; } post.Categories = categories.Elements("category").Select(node => node.Value).ToArray(); }
private static T GetElementValue <T>(this XContainer xElement, string childElementName, T defaultvalue) { var childElement = xElement.Element(childElementName); if (childElement == null) { return(defaultvalue); } return(DeserializeValue(childElement.Value, defaultvalue)); }
/// <summary> /// Ruft ein notwendiges Element ab. /// </summary> /// <param name="xContainer">Container in dem das Element enthalten ist.</param> /// <param name="xname">Name des Elements</param> /// <returns>Das erste Element mit dem gewünschten Namen.</returns> /// <exception cref="XObjectException">Falls keine Element mit dem Namen exisitert.</exception> public static XElement RequiredElement(this XContainer xContainer, XName xname) { var element = xContainer.Element(xname); if (element == null) { throw new XObjectException($"Notwendiges Element {xname} fehlt.", xContainer); } return(element); }
private static void InsertItemPrice(XContainer item, IEnumerable <XElement> toList, IReadOnlyDictionary <string, IChain> chainList, IReadOnlyDictionary <string, double> userItems) { toList.ToList().ForEach(itemCode => { var newItem = new Item(item.Element("ItemName")?.Value, userItems[item?.Element("ItemName")?.Value]); AddItemToChain(itemCode.Attribute("name").Value, itemCode.Attribute("code").Value, newItem, chainList); }); }
//[Variation(Priority = 2, Desc = "XElement.AncestorsAndSelf(XName) I.", Param = true)] //[Variation(Priority = 2, Desc = "XElement.AncestorsAndSelf(XName) II.", Param = false)] #region Methods private void VerifyDeleteNodes <T>(IEnumerable <T> toRemove) where T : XNode { // copy of the data to delete IEnumerable <XNode> toRemoveCopy = toRemove.OfType <XNode>().ToList(); // Create array of parents IEnumerable <XContainer> parents = toRemove.Select(x => (x == null) ? (XContainer)null : (x.Parent != null ? (XContainer)x.Parent : (XContainer)x.Document)).ToList(); // calculate the expected results for the parents of the processed elements var expectedNodesForParent = new Dictionary <XContainer, List <ExpectedValue> >(); foreach (XContainer p in parents) { if (p != null) { expectedNodesForParent.TryAdd(p, p.Nodes().Except(toRemoveCopy.Where(x => x != null)).Select(a => new ExpectedValue(!(a is XText), a)).ProcessNodes().ToList()); } } toRemove.Remove(); IEnumerator <XNode> copyToRemove = toRemoveCopy.GetEnumerator(); IEnumerator <XContainer> parentsEnum = parents.GetEnumerator(); // verify on parents: deleted elements should not be found while (copyToRemove.MoveNext() && parentsEnum.MoveNext()) { XNode node = copyToRemove.Current; if (node != null) { XContainer parent = parentsEnum.Current; TestLog.Compare(node.Parent, null, "Parent of deleted"); TestLog.Compare(node.Document, null, "Document of deleted"); TestLog.Compare(node.NextNode, null, "NextNode of deleted"); TestLog.Compare(node.PreviousNode, null, "PreviousNode of deleted"); if (parent != null) { TestLog.Compare(parent.Nodes().Where(x => x == node).IsEmpty(), "Nodes axis"); if (node is XElement) { var e = node as XElement; e.Verify(); TestLog.Compare(parent.Element(e.Name) != node, "Element axis"); TestLog.Compare(parent.Elements(e.Name).Where(x => x == e).IsEmpty(), "Elements axis"); } // Compare the rest of the elements TestLog.Compare(expectedNodesForParent[parent].EqualAll(parent.Nodes(), XNode.EqualityComparer), "The rest of the nodes"); } } } }
public static XElement GetOrAddElement(this XContainer container, XName name) { XElement xelement = container.Element(name); if (xelement == null) { xelement = new XElement(name); container.Add((object)xelement); } return(xelement); }
private static XContainer GrabContainer(this XContainer parent, XName name) { var element = parent.Element(name); if (element == null) { element = new XElement(name); parent.Add(element); } return(element); }
protected static string ReadValueStr(XContainer securityInfoNode, string propertyName) { if (securityInfoNode.Element(propertyName) != null) { var rtn = securityInfoNode.Element(propertyName).Value; if (!string.IsNullOrEmpty(rtn)) { return(rtn.Trim()); } else { return(null); } } else { return(null); // throw new System.Xml.XmlException(string.Format("未找到节点{0}", propertyName)); } }
/// <summary> /// Returns the element with the specified name, or creates and adds one if required. /// </summary> /// <param name="parent"></param> /// <param name="name"></param> /// <returns></returns> public static XElement ElementOrAdd(this XContainer parent, XName name) { var e = parent.Element(name); if (e == null) { parent.Add(e = new XElement(name)); } return(e); }