本文整理汇总了C#中XElement.Elements方法的典型用法代码示例。如果您正苦于以下问题:C# XElement.Elements方法的具体用法?C# XElement.Elements怎么用?C# XElement.Elements使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类XElement
的用法示例。
在下文中一共展示了XElement.Elements方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FromXElement
public static void FromXElement(this TreeView T_View, XElement XElem , TreeNode ParentNode = null)
{
// Create temporarty node
var TempNode = new TreeNode(XElem.Name.LocalName);
// Determine if recursive loop or initial entry
if(ParentNode == null)
// add root of tree
T_View.Nodes.Add(TempNode);
// add to parent tree node
else ParentNode.Nodes.Add(TempNode);
// for each attribute in the element
foreach(XAttribute XAttr in XElem.Attributes()){
// create a tree node for the attribute
var AttrNode = new TreeNode(XAttr.Name.LocalName);
// add the value of the attribute as tree node
AttrNode.Nodes.Add(new TreeNode(XAttr.Value));
// add the node to the tree
TempNode.Nodes.Add(AttrNode);
}
// if Element has no child elements
if(!XElem.HasElements && !XElem.Value.Equals(string.Empty)) {
// add a node for the value if it hase one
var ValueNode = new TreeNode("value");
// add the value as a node
ValueNode.Nodes.Add(new TreeNode(XElem.Value));
// add the node to tree
TempNode.Nodes.Add(ValueNode);
}
if(XElem.HasElements) {
// Make element parent node
var TempParentNode = TempNode;
// recurse
XElem.Elements().ForEach(XEChild => FromXElement(T_View, XEChild, TempParentNode));
}
}
示例2: ElementsWithXNameOnXElementBeforeAndAfter
public static void ElementsWithXNameOnXElementBeforeAndAfter()
{
XElement a = new XElement("A", "a"), b = new XElement("B", "b");
IEnumerable<XElement> nodes = a.Elements("B");
Assert.Equal(0, nodes.Count());
a.Add(b, b, b, b);
Assert.Equal(4, nodes.Count());
}
示例3: AddTraceSource
private void AddTraceSource (IDictionary d, Hashtable sources, XElement element)
{
string name = null;
SourceLevels levels = SourceLevels.Error;
StringDictionary atts = new StringDictionary ();
foreach (XAttribute a in element.Attributes())
{
switch (a.Name)
{
case "name":
name = a.Value;
break;
case "switchValue":
levels = (SourceLevels)Enum.Parse (typeof (SourceLevels), a.Value, false);
break;
default:
atts[a.Name] = a.Value;
break;
}
}
if (name == null)
throw new ConfigurationException ("Mandatory attribute 'name' is missing in 'source' element.");
// ignore duplicate ones (no error occurs)
if (sources.ContainsKey (name))
return;
TraceSourceInfo sinfo = new TraceSourceInfo (name, levels, configValues);
sources.Add (sinfo.Name, sinfo);
foreach (XElement child in element.Elements())
{
XmlNodeType t = child.NodeType;
if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
continue;
if (t == XmlNodeType.Element)
{
if (child.Name == "listeners")
AddTraceListeners (d, child, sinfo.Listeners);
else
ThrowUnrecognizedElement (child);
ValidateInvalidAttributes (child.Attributes ().ToDictionary (a => a.Name), child);
}
else
ThrowUnrecognizedNode (child);
}
}
示例4: AddTraceListeners
// only defines "add" and "remove", but "clear" also works
// for add, "name" is required; initializeData is optional; "type" is required in 1.x, optional in 2.0.
private void AddTraceListeners (IDictionary d, XElement listenersNode, TraceListenerCollection listeners)
{
#if !TARGET_JVM
// There are no attributes on <listeners/>
ValidateInvalidAttributes (listenersNode.Attributes ().ToDictionary (a=> a.Name), listenersNode);
foreach (XElement child in listenersNode.Elements ())
{
XmlNodeType t = child.NodeType;
if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
continue;
if (t == XmlNodeType.Element)
{
IDictionary<string, XAttribute> attributes = child.Attributes ().ToDictionary (a => a.Name);
string name = null;
switch (child.Name)
{
case "add":
AddTraceListener (d, child, attributes, listeners);
break;
case "remove":
name = GetAttribute (attributes, "name", true, child);
RemoveTraceListener (name);
break;
case "clear":
configValues.Listeners.Clear ();
break;
default:
ThrowUnrecognizedElement (child);
break;
}
ValidateInvalidAttributes (attributes, child);
}
else
ThrowUnrecognizedNode (child);
}
#endif
}
示例5: AddTraceElement
private void AddTraceElement (IDictionary d, XElement element)
{
AddTraceAttributes (d, element);
foreach (XElement child in element.Elements ())
{
XmlNodeType t = child.NodeType;
if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
continue;
if (t == XmlNodeType.Element)
{
if (child.Name == "listeners")
AddTraceListeners (d, child, configValues.Listeners);
else
ThrowUnrecognizedElement (child);
ValidateInvalidAttributes (child.Attributes ().ToDictionary (a => a.Name), child);
}
else
ThrowUnrecognizedNode (child);
}
}
示例6: AddSourcesElement
private void AddSourcesElement (IDictionary d, XElement element)
{
// FIXME: are there valid attributes?
ValidateInvalidAttributes (element.Attributes ().ToDictionary (a => a.Name), element);
Hashtable sources = d["sources"] as Hashtable;
if (sources == null)
{
sources = new Hashtable ();
d["sources"] = sources;
}
foreach (XElement child in element.Elements ())
{
XmlNodeType t = child.NodeType;
if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
continue;
if (t == XmlNodeType.Element)
{
if (child.Name == "source")
AddTraceSource (d, sources, child);
else
ThrowUnrecognizedElement (child);
// ValidateInvalidAttributes (child.Attributes, child);
}
else
ThrowUnrecognizedNode (child);
}
}
示例7: AddAssertElement
// Remarks: Both attribute are optional
private void AddAssertElement (IDictionary d, XElement element)
{
IDictionary<string, XAttribute> c = element.Attributes ().ToDictionary (a => a.Name);
string assertuienabled = GetAttribute (c, "assertuienabled", false, element);
string logfilename = GetAttribute (c, "logfilename", false, element);
ValidateInvalidAttributes (c, element);
if (assertuienabled != null)
{
try
{
d["assertuienabled"] = bool.Parse (assertuienabled);
}
catch (Exception e)
{
throw new ConfigurationException ("The `assertuienabled' attribute must be `true' or `false'",
e, element);
}
}
if (logfilename != null)
d["logfilename"] = logfilename;
DefaultTraceListener dtl = (DefaultTraceListener)configValues.Listeners["Default"];
if (dtl != null)
{
if (assertuienabled != null)
dtl.AssertUiEnabled = (bool)d["assertuienabled"];
if (logfilename != null)
dtl.LogFileName = logfilename;
}
if (element.HasElements)
ThrowUnrecognizedElement (element.Elements ().First ());
}
示例8: AddSwitchesElement
// name and value attributes are required
// Docs do not define "remove" or "clear" elements, but .NET recognizes
// them
private void AddSwitchesElement (IDictionary d, XElement element)
{
#if !TARGET_JVM
// There are no attributes on <switch/>
ValidateInvalidAttributes (element.Attributes ().ToDictionary (a => a.Name), element);
IDictionary newNodes = new Hashtable ();
foreach (XElement child in element.Elements ())
{
XmlNodeType t = child.NodeType;
if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
continue;
if (t == XmlNodeType.Element)
{
IDictionary<string, XAttribute> attributes = child.Attributes ().ToDictionary (a => a.Name);
string name = null;
string value = null;
switch (child.Name)
{
case "add":
name = GetAttribute (attributes, "name", true, child);
value = GetAttribute (attributes, "value", true, child);
newNodes[name] = GetSwitchValue (name, value);
break;
case "remove":
name = GetAttribute (attributes, "name", true, child);
newNodes.Remove (name);
break;
case "clear":
newNodes.Clear ();
break;
default:
ThrowUnrecognizedElement (child);
break;
}
ValidateInvalidAttributes (attributes, child);
}
else
ThrowUnrecognizedNode (child);
}
d[element.Name] = newNodes;
#endif
}
示例9: LoadSavToPawn
public override void LoadSavToPawn(PawnData pawn, XElement xElement, SavSlot savSlot)
{
int i = 0;
foreach (XElement childElement in xElement.Elements())
{
if (i >= Keys.Count)
{
throw new XmlException(string.Format(
"Array {0} in save config is missing a child class",
Name));
}
if (Keys[i].Length > 0)
{
PawnParameter pawnParameter = pawn.GetOrAddParameter(Keys[i]);
if (pawnParameter != null)
{
pawnParameter.Value = childElement.GetParsedValueAttribute();
}
}
++i;
}
}
示例10: Create
public virtual object Create (object parent, object configContext, XElement section)
{
IDictionary d;
if (parent == null)
d = new Hashtable (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
else
d = (IDictionary)((ICloneable)parent).Clone ();
if (d.Contains (TraceImplSettings.Key))
configValues = (TraceImplSettings)d[TraceImplSettings.Key];
else
d.Add (TraceImplSettings.Key, configValues = new TraceImplSettings ());
// process <sharedListeners> first
foreach (XElement child in section.Elements ())
{
switch (child.NodeType)
{
case XmlNodeType.Element:
if (child.Name != "sharedListeners")
continue;
AddTraceListeners (d, child, GetSharedListeners (d));
break;
}
}
foreach (XElement child in section.Elements ())
{
XmlNodeType type = child.NodeType;
switch (type)
{
/* ignore */
case XmlNodeType.Whitespace:
case XmlNodeType.Comment:
continue;
case XmlNodeType.Element:
if (child.Name == "sharedListeners")
continue;
ElementHandler eh = (ElementHandler)elementHandlers[child.Name];
if (eh != null)
eh (d, child);
else
ThrowUnrecognizedElement (child);
break;
default:
ThrowUnrecognizedElement (child);
break;
}
}
return d;
}
示例11: LoadPawnToSav
public override void LoadPawnToSav(PawnData pawn, XElement xElement, SavSlot savSlot)
{
int i = 0;
foreach (XElement childElement in xElement.Elements())
{
if (i >= Keys.Count)
{
throw new XmlException(string.Format(
"An array in the save config is missing a key " +
"for the following element: {0}\n" +
"Array's first key is: {1}",
childElement.ToString(),
Keys.Count > 0 ? Keys[0] : ""));
}
if (Keys[i].Length > 0)
{
PawnParameter pawnParameter = pawn.GetParameter(Keys[i]);
if (pawnParameter != null)
{
LoadParameterToSav(pawnParameter, childElement);
}
}
++i;
}
}
示例12: LoadSavNameToPawn
private void LoadSavNameToPawn(PawnData pawn, XElement xElement)
{
StringBuilder sb = new StringBuilder();
try
{
foreach (XElement letterElement in xElement.Elements())
{
long value = letterElement.GetParsedValueAttribute();
if (value == 0)
{
break;
}
sb.Append((char)value);
}
pawn.GetOrAddParameter(Key).Value = sb.ToString();
}
catch (Exception ex)
{
throw new XmlException(".sav file contained an invalid Pawn name.", ex);
}
}
示例13: LoadPawnNameToSav
private void LoadPawnNameToSav(PawnData pawn, XElement xElement)
{
string name = pawn.GetParameter(Key).Value as string;
int letterIndex = 0;
foreach (XElement letterElement in xElement.Elements())
{
XAttribute letterAttribute = letterElement.GetValueAttribute();
if (letterIndex < name.Length)
{
letterAttribute.Value = ((int)name[letterIndex]).ToString();
++letterIndex;
}
else if (letterAttribute.Value == "0")
{
break;
}
else
{
letterAttribute.Value = "0";
}
}
}
示例14: XmlToJObject
static JObject XmlToJObject(XElement node)
{
JObject jObj = new JObject();
foreach (var attr in node.Attributes())
{
jObj.Add(attr.Name.LocalName, attr.Value);
}
foreach (var childs in node.Elements().GroupBy(x => x.Name.LocalName))
{
string name = childs.ElementAt(0).Name.LocalName;
if (childs.Count() > 1)
{
JArray jArray = new JArray();
foreach (var child in childs)
{
jArray.Add(XmlToJObject(child));
}
jObj.Add(name, jArray);
}
else
{
jObj.Add(name, XmlToJObject(childs.ElementAt(0)));
}
}
node.Elements().Remove();
if (!String.IsNullOrEmpty(node.Value))
{
string name = "Value";
while (jObj[name] != null) name = "_" + name;
jObj.Add(name, node.Value);
}
return jObj;
}