本文整理汇总了C#中XElement.Attributes方法的典型用法代码示例。如果您正苦于以下问题:C# XElement.Attributes方法的具体用法?C# XElement.Attributes怎么用?C# XElement.Attributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类XElement
的用法示例。
在下文中一共展示了XElement.Attributes方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: AttributeWithXNameBeforeAndAfter
public static void AttributeWithXNameBeforeAndAfter()
{
XElement a = new XElement("A", "a");
IEnumerable<XAttribute> nodes = a.Attributes("name");
Assert.Equal(0, nodes.Count());
a.Add(new XAttribute("name", "a"), new XAttribute("type", "alphabet"));
Assert.Equal(1, 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: 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);
}
}
示例6: AddTraceAttributes
// all attributes are optional
private void AddTraceAttributes (IDictionary d, XElement element)
{
IDictionary<string, XAttribute> c = element.Attributes ().ToDictionary (a => a.Name);
string autoflushConf = GetAttribute (c, "autoflush", false, element);
string indentsizeConf = GetAttribute (c, "indentsize", false, element);
ValidateInvalidAttributes (c, element);
if (autoflushConf != null)
{
bool autoflush = false;
try
{
autoflush = bool.Parse (autoflushConf);
d["autoflush"] = autoflush;
}
catch (Exception e)
{
throw new ConfigurationException ("The `autoflush' attribute must be `true' or `false'",
e, element);
}
configValues.AutoFlush = autoflush;
}
if (indentsizeConf != null)
{
int indentsize = 0;
try
{
indentsize = int.Parse (indentsizeConf);
d["indentsize"] = indentsize;
}
catch (Exception e)
{
throw new ConfigurationException ("The `indentsize' attribute must be an integral value.",
e, element);
}
configValues.IndentSize = indentsize;
}
}
示例7: 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
}
示例8: 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 ());
}
示例9: 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;
}