本文整理匯總了C#中System.Xml.Linq.XElement.Attributes方法的典型用法代碼示例。如果您正苦於以下問題:C# XElement.Attributes方法的具體用法?C# XElement.Attributes怎麽用?C# XElement.Attributes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Xml.Linq.XElement
的用法示例。
在下文中一共展示了XElement.Attributes方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ProcessElement
public void ProcessElement(XElement element)
{
if (!IsEnabled) return;
if (!element.HasAttributes) return;
// Setter? Format "Value" attribute if "Property" atribute matches ThicknessAttributeNames
if (element.Name == SetterName)
{
var propertyAttribute = element.Attributes("Property").FirstOrDefault();
if (propertyAttribute != null && ThicknessAttributeNames.Any(match => match.IsMatch(propertyAttribute.Value)))
{
var valueAttribute = element.Attributes("Value").FirstOrDefault();
if (valueAttribute != null)
{
FormatAttribute(valueAttribute);
}
}
}
// Not setter. Format value of all attributes where attribute name matches ThicknessAttributeNames
else
{
foreach (var attribute in element.Attributes())
{
var isMatchingAttribute = ThicknessAttributeNames.Any(match => match.IsMatch(attribute.Name));
if (isMatchingAttribute)
{
FormatAttribute(attribute);
}
}
}
}
示例2: BasicValueTypeAttributeCheckIsValid
private static bool BasicValueTypeAttributeCheckIsValid(XElement element, bool isCustomProperty, out string value)
{
Guard.AgainstNull(element, "element");
if (isCustomProperty)
{
if (element.Attributes().Count() != 2)
{
value = "Wrong number of attributes";
return false;
}
if (element.Attribute(SharedConstants.Name) == null)
{
value = "Custom property has no 'name' attribute";
return false;
}
}
else
{
if (element.Attributes().Count() != 1)
{
value = "Wrong number of attributes";
return false;
}
}
value = element.Attribute(SharedConstants.Val).Value;
return true;
}
示例3: GetRegexes
MatchFactory GetRegexes(XElement element) {
switch (element.Name.LocalName) {
case "MatchHonorees":
if (Presentation.MelaveMalka == null)
return FixedRegexes();
return FixedRegexes(Presentation.MelaveMalka.Honorees.SelectMany(AdVerifier.GetNameRegexes));
case "MatchDonors":
return ad => ad.Row.Pledges.SelectMany(p => AdVerifier.GetNameRegexes(p.Person));
case "Match":
return FixedRegexes(new Regex(element.Attribute("Regex").Value));
case "MatchPerson":
foreach (var field in element.Attributes())
if (!Person.Schema.Columns.Contains(field.Name.LocalName))
throw new ConfigurationException($"Unexpected attribute {field} in <MatchPerson>");
var fields = element.Attributes().ToDictionary(
a => a.Name.LocalName,
a => a.Value
);
var people = Program.Table<Person>().Rows.Where(p => fields.All(f => f.Value.Equals(p[f.Key])));
if (people.Has(2))
throw new ConfigurationException($"Format rule {element} matches multiple people: {people.Join(", ", p => p.VeryFullName)}");
var person = people.FirstOrDefault();
if (person == null)
throw new ConfigurationException($"Format rule {element} doesn't match anyone in the master directory.");
return FixedRegexes(AdVerifier.GetNameRegexes(person));
case "Format": // Ignore this element.
return FixedRegexes();
default:
throw new ConfigurationException($"Unexpected <{element.Name}> element in <FormatRule>.");
}
}
示例4: GetLocatorPredicate
public static string GetLocatorPredicate(XElement element)
{
var locatorAttribute = element.Attributes(Namespaces.Xdt + "Locator").FirstOrDefault();
if (locatorAttribute == null)
{
return String.Empty;
}
else
{
var locator = Locator.Parse(locatorAttribute.Value);
if (locator.Type == "Condition")
{
// use the user-defined value as an xpath predicate
return "[" + locator.Arguments + "]";
}
else if (locator.Type == "Match")
{
// convenience case of the Condition locator, build the xpath
// predicate for the user by matching on all specified attributes
var attributeNames = locator.Arguments.Split(',').Select(s => s.Trim());
var attributes = element.Attributes().Where(a => attributeNames.Contains(a.Name.LocalName));
return "[" + attributes.ToConcatenatedString(a =>
"@" + a.Name.LocalName + "='" + a.Value + "'", " and ") + "]";
}
else
{
throw new NotImplementedException(String.Format("The Locator '{0}' is not supported", locator.Type));
}
}
}
示例5: ElementToLevelText
private static string ElementToLevelText(XElement el)
{
var blueText = el.Attributes().Any( a => a.Name == "Text") ? el.Attribute("Text").Value : string.Empty;
var blueAmount = el.Attributes().Any(a => a.Name == "AMOUNT") ? el.Attribute("AMOUNT").Value : string.Empty;
var blueType = el.Attributes().Any(a => a.Name == "TYPE") ? el.Attribute("TYPE").Value : string.Empty;
return blueText +" : "+ blueAmount + " " + blueType;
}
示例6: DslBuilder
public DslBuilder(XElement builderClassNode, XmlDocCommentReader commentReader, Type builderType)
{
input = builderClassNode.Value;
syntaxStateClassname = builderClassNode.Attributes().Single(f => f.Name == "name").Value;
outputNamespace = builderClassNode.Attributes().Single(f => f.Name == "namespace").Value;
type = builderType;
this.commentReader = commentReader;
}
示例7: SetMathVariant
void SetMathVariant(XElement mathVariant)
{
string name = mathVariant.Attribute("name").Value;
string weight = mathVariant.Attributes("weight").FirstOrDefault() != null ? mathVariant.Attribute("weight").Value : "normal";
string style = mathVariant.Attributes("style").FirstOrDefault() != null ? mathVariant.Attribute("style").Value : "normal";
MathVariant mv = new MathVariant(name, weight, style);
mathVariant.Attribute("family").Value.Split(',').Select(x => x.Trim()).ToList().ForEach(x=> mv.AddFamily(x));
Variants.Add(name, mv);
}
示例8: UpdateElement
/// <summary>
/// Updates an element from the datasource.
/// </summary>
/// <param name="table"></param>
/// <param name="element"></param>
public void UpdateElement(IList<XElement> table, XElement element)
{
XElement orderToUpdate = (from order in table
where order.Attribute(element.Attributes().FirstOrDefault().Name).Value == element.Attributes().FirstOrDefault().Value
select order).FirstOrDefault();
foreach (XElement el in orderToUpdate.Descendants())
{
el.Value = element.Element(el.Name).Value;
}
}
示例9: ContainerAdd
/// <summary>
/// Tests the Add methods on Container.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
//[Variation(Desc = "ContainerAdd")]
public void ContainerAdd()
{
XElement element = new XElement("foo");
// Adding null does nothing.
element.Add(null);
Validate.Count(element.Nodes(), 0);
// Add node, attrbute, string, some other value, and an IEnumerable.
XComment comment = new XComment("this is a comment");
XComment comment2 = new XComment("this is a comment 2");
XComment comment3 = new XComment("this is a comment 3");
XAttribute attribute = new XAttribute("att", "att-value");
string str = "this is a string";
int other = 7;
element.Add(comment);
element.Add(attribute);
element.Add(str);
element.Add(other);
element.Add(new XComment[] { comment2, comment3 });
Validate.EnumeratorDeepEquals(
element.Nodes(),
new XNode[] { comment, new XText(str + other), comment2, comment3 });
Validate.EnumeratorAttributes(element.Attributes(), new XAttribute[] { attribute });
element.RemoveAll();
Validate.Count(element.Nodes(), 0);
// Now test params overload.
element.Add(comment, attribute, str, other);
Validate.EnumeratorDeepEquals(
element.Nodes(),
new XNode[] { comment, new XText(str + other) });
Validate.EnumeratorAttributes(element.Attributes(), new XAttribute[] { attribute });
// Not allowed to add a document as a child.
XDocument document = new XDocument();
try
{
element.Add(document);
Validate.ExpectedThrow(typeof(ArgumentException));
}
catch (Exception ex)
{
Validate.Catch(ex, typeof(ArgumentException));
}
}
示例10: CreateChildElement
private static dynamic CreateChildElement(XElement parent)
{
if (parent.Attributes().Count() == 0 && parent.Elements().Count() == 0)
return null;
IDictionary<string, object> child = new ExpandoObject();
parent.Attributes().ToList().ForEach(attr =>
{
child.Add(attr.Name.LocalName, attr.Value);
if (!child.ContainsKey("NodeName"))
child.Add("NodeName", attr.Parent.Name.LocalName);
});
parent.Elements().ToList().ForEach(childElement =>
{
var grandChild = CreateChildElement(childElement);
if (grandChild != null)
{
string nodeName = grandChild.NodeName;
if (child.ContainsKey(nodeName) && child[nodeName].GetType() != typeof(List<dynamic>))
{
var firstValue = child[nodeName];
child[nodeName] = new List<dynamic>();
((dynamic)child[nodeName]).Add(firstValue);
((dynamic)child[nodeName]).Add(grandChild);
}
else if (child.ContainsKey(nodeName) && child[nodeName].GetType() == typeof(List<dynamic>))
{
((dynamic)child[nodeName]).Add(grandChild);
}
else
{
child.Add(childElement.Name.LocalName, CreateChildElement(childElement));
if (!child.ContainsKey("NodeName"))
child.Add("NodeName", parent.Name.LocalName);
}
}
else
{
child.Add(childElement.Name.LocalName, childElement.Value);
if (!child.ContainsKey("NodeName"))
child.Add("NodeName", parent.Name.LocalName);
}
});
return child;
}
示例11: KevinBaconItem
public KevinBaconItem(XElement element)
{
_actorName = element.Attributes("name").First().Value + " (" + element.Elements("link").Count() + ")";
_description = element.Attributes("name").First().Value;
var links = element.Elements("link").Select(linkElement => " to " + linkElement.Attributes("actor").First().Value
+ " in " + linkElement.Attributes("movie").First().Value);
_description += "\r\n";
foreach (var link in links)
{
_description += "\r\n" + link;
}
_description += "\r\n";
}
示例12: PendingUpdate
public PendingUpdate(XElement updateXml)
{
var rawVersion = updateXml
.Attributes("NewVersion")
.Select(a => a.Value)
.FirstOrDefault();
NewVersion = new Version(rawVersion);
var rawUpdateFileUri = updateXml
.Attributes("UpdateFileUri")
.Select(a => a.Value)
.FirstOrDefault();
UpdateFileUri = new Uri(rawUpdateFileUri);
var rawInfoFileUri = updateXml
.Attributes("UpdateInfoUri")
.Select(a => a.Value)
.FirstOrDefault();
if (rawInfoFileUri != null)
{
UpdateInfoUri = new Uri(rawInfoFileUri);
}
var rawCategories = updateXml
.Attributes("Categories")
.Select(a => a.Value)
.FirstOrDefault();
Categories = rawCategories == null
? new List<string>()
: new List<string>(rawCategories.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
InstallFileName = updateXml
.Attributes("InstallFileName")
.Select(a => a.Value)
.FirstOrDefault();
var descriptionNode = updateXml
.Elements("Description")
.FirstOrDefault();
if (descriptionNode != null)
{
Description = descriptionNode.Value;
}
}
示例13: CreateFieldRefOperand
public IOperand CreateFieldRefOperand(XElement el)
{
if (el == null)
{
throw new ArgumentNullException("el");
}
Guid? id = null;
var idAttr = el.Attributes().FirstOrDefault(a => a.Name == Attributes.ID);
if (idAttr != null)
{
try
{
id = new Guid(idAttr.Value);
}
catch
{
throw new CamlAnalysisException(string.Format("Value '{0}' is not correct for attribute '{1}'", idAttr.Value, Attributes.ID));
}
}
string name = null;
var nameAttr = el.Attributes().FirstOrDefault(a => a.Name == Attributes.Name);
if (nameAttr != null)
{
name = nameAttr.Value;
}
if (id != null && !string.IsNullOrEmpty(name))
{
throw new CamlAnalysisException(string.Format("Only one from two attributes should be specified: {0} or {1}", Attributes.ID, Attributes.Name));
}
if (id == null && string.IsNullOrEmpty(name))
{
throw new CamlAnalysisException(string.Format("At least one from two attributes should be specified: {0} or {1}", Attributes.ID, Attributes.Name));
}
var attributes = el.Attributes().Where(
attr =>
{
return (attr.Name != Attributes.ID && attr.Name != Attributes.Name &&
!string.IsNullOrEmpty(attr.Value));
})
.Select(attr => new KeyValuePair<string, string>(attr.Name.ToString(), attr.Value))
.ToList();
return (id != null ? new FieldRefOperand(id.Value, attributes) : new FieldRefOperand(name, attributes));
}
示例14: LoadMPD
public static MPD LoadMPD(XElement element)
{
var ns = element.GetDefaultNamespace().NamespaceName;
var result = new MPD();
result.Id = (string)element.Attribute("id");
result.Profiles = (string)element.Attribute("profiles");
result.Type = element.Attribute("type").GetEnum<Presentation>();
result.AvailabilityStartTime = element.Attribute("availabilityStartTime").GetNullableDateTime();
result.AvailabilityEndTime = element.Attribute("availabilityEndTime").GetNullableDateTime();
result.MediaPresentationDuration = element.Attribute("mediaPresentationDuration").GetNullableDuration();
result.MinimumUpdatePeriod = element.Attribute("minimumUpdatePeriod").GetNullableDuration();
result.MinBufferTime = element.Attribute("minBufferTime").GetNullableDuration();
result.TimeShiftBufferDepth = element.Attribute("timeShiftBufferDepth").GetNullableDuration();
result.SuggestedPresentationDelay = element.Attribute("suggestedPresentationDelay").GetNullableDuration();
result.MaxSegmentDuration = element.Attribute("maxSegmentDuration").GetNullableDuration();
result.MaxSubsegmentDuration = element.Attribute("maxSubsegmentDuration").GetNullableDuration();
result.AnyAttr.AddRange(element.Attributes());
result.ProgramInformation.AddRange(element.Elements(XName.Get("ProgramInformation", ns)).Select(LoadProgramInformation));
result.BaseURL.AddRange(element.Elements(XName.Get("BaseURL", ns)).Select(LoadBaseURL));
result.Location.AddRange(element.Elements(XName.Get("Location", ns)).Select(e => e.Value));
result.Period.AddRange(element.Elements(XName.Get("Period", ns)).Select(LoadPeriod));
result.Metrics.AddRange(element.Elements(XName.Get("Metrics", ns)).Select(LoadMetrics));
result.Any.AddRange(element.Elements());
return result;
}
示例15: HasIncludePath
private static bool HasIncludePath(XElement includeElement, string includePath)
{
XAttribute attr;
attr = includeElement.Attributes(PathAttrName).Single();
return attr != null && string.Equals(attr.Value, includePath, StringComparison.OrdinalIgnoreCase);
}