本文整理汇总了C#中System.Xml.Linq.XDocument.XPathEvaluate方法的典型用法代码示例。如果您正苦于以下问题:C# XDocument.XPathEvaluate方法的具体用法?C# XDocument.XPathEvaluate怎么用?C# XDocument.XPathEvaluate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XDocument
的用法示例。
在下文中一共展示了XDocument.XPathEvaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ModifyConfig
public override void ModifyConfig(XDocument doc)
{
// Add the new audit config section, if the ForwardReceivedMessagesTo attribute has not been set in the UnicastBusConfig.
var frmAttributeEnumerator = (IEnumerable)doc.XPathEvaluate("/configuration/UnicastBusConfig/@ForwardReceivedMessagesTo");
var isForwardReceivedMessagesAttributeDefined = frmAttributeEnumerator.Cast<XAttribute>().Any();
// Then add the audit config
var sectionElement =
doc.XPathSelectElement(
"/configuration/configSections/section[@name='AuditConfig' and @type='NServiceBus.Config.AuditConfig, NServiceBus.Core']");
if (sectionElement == null)
{
if (isForwardReceivedMessagesAttributeDefined)
doc.XPathSelectElement("/configuration/configSections").Add(new XComment(exampleAuditConfigSection));
else
doc.XPathSelectElement("/configuration/configSections").Add(new XElement("section",
new XAttribute("name",
"AuditConfig"),
new XAttribute("type",
"NServiceBus.Config.AuditConfig, NServiceBus.Core")));
}
var forwardingElement = doc.XPathSelectElement("/configuration/AuditConfig");
if (forwardingElement == null)
{
doc.Root.LastNode.AddAfterSelf(new XComment(Instructions),
isForwardReceivedMessagesAttributeDefined ? (object) new XComment(@"Since we detected that you already have forwarding setup we haven't enabled the audit feature.
Please remove the ForwardReceivedMessagesTo attribute from the UnicastBusConfig and uncomment the AuditConfig section.
<AuditConfig QueueName=""audit"" />") : new XElement("AuditConfig", new XAttribute("QueueName", "audit")));
}
}
示例2: GetSingleValuedStringFromXPath
internal string GetSingleValuedStringFromXPath(XDocument document, string xpath)
{
if (document == null) { throw new ArgumentNullException("document"); }
if (string.IsNullOrEmpty(xpath)) { throw new ArgumentNullException("xpath"); }
IEnumerable attributes = document.XPathEvaluate(xpath) as IEnumerable;
return attributes.Cast<XAttribute>().Single().Value;
}
示例3: GetXmlDocumentation
/// <summary>
/// Returns the XML documentation (summary tag) for the specified member.
/// </summary>
/// <param name="member">The reflected member.</param>
/// <param name="xml">XML documentation.</param>
/// <returns>The contents of the summary tag for the member.</returns>
public static string GetXmlDocumentation(this MemberInfo member, XDocument xml)
{
return xml.XPathEvaluate(
String.Format(
"string(/doc/members/member[@name='{0}']/summary)",
GetMemberElementName(member)
)
).ToString().Trim();
}
示例4: GetXmlDocumentation
public static string GetXmlDocumentation(this TypedParameter parameter, XDocument xml)
{
if (xml == null) return String.Empty;
return xml.XPathEvaluate(
String.Format(
"string(/doc/members/member[@name='{0}']/param[@name='{1}'])",
GetMemberElementName(parameter.Function),
parameter.Name )
).ToString().Trim();
}
示例5: GetSearchTags
public static IEnumerable<string> GetSearchTags(this FunctionDescriptor member, XDocument xml)
{
if (xml == null) return new List<string>();
return xml.XPathEvaluate(
String.Format(
"string(/doc/members/member[@name='{0}']/search)",
GetMemberElementName(member)
)
).ToString().Split(',').Select(x => x.Trim()).Where(x => x != String.Empty);
}
示例6: ValidatePath
private string ValidatePath(XDocument document, XmlNamespaceManager xnm, string prefix)
{
try
{
var nsxpath = XPath.QualifyXPath(prefix);
var check = ((IEnumerable)document.XPathEvaluate(nsxpath, xnm)).Cast<XObject>().FirstOrDefault();
return check != null ? string.Empty : XPath;
}
catch (Exception ex)
{
return XPath + ": " + ex.Message;
}
}
示例7: getWeatherJSON
public string getWeatherJSON(string location)
{
string str = "";
try
{
httpReq = (HttpWebRequest)WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?q=" + location + "&mode=xml"); // starts a web request to an api hosted by openweathermap which returns an xml document with weather data
response = (HttpWebResponse)httpReq.GetResponse(); // recieves the response
readStream = response.GetResponseStream(); // the stream reader reads the response
streamreader = new StreamReader(readStream, Encoding.UTF8); // sets it to a stream reader with the UTF8 encoding
responseString = streamreader.ReadToEnd(); // reads ther esponse to a string
XMLDoc = XDocument.Parse(responseString); // parses the recieved document into an object intended for working with xml documents
IEnumerable att = (IEnumerable)XMLDoc.XPathEvaluate("/current/weather/@icon"); // the node we are interested in
Console.WriteLine(att.Cast<XAttribute>().FirstOrDefault()); // debug info
str = att.Cast<XAttribute>().FirstOrDefault().ToString(); // sets the string str to the found node
string[] results = str.Split('"'); // splits it by the " character to get the data we are after split up
return results[1];
}
catch (System.Net.WebException e) // if an exception was thrown, this will execute
{
return e.ToString();
}
//string responseWeather = returnIcon(responseString);
}
示例8: PatchDtsIds
private static XDocument PatchDtsIds(XDocument document)
{
var dtsIdDictionary = new Dictionary<string, string>();
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(new NameTable());
xmlnsManager.AddNamespace("DTS", "www.microsoft.com/SqlServer/Dts");
foreach (var dtsIdElement in document.XPathSelectElements("//DTS:Property[@DTS:Name='DTSID']", xmlnsManager))
{
XElement nameElement = dtsIdElement.Parent.XPathSelectElement("./DTS:Property[@DTS:Name='ObjectName']", xmlnsManager);
if (nameElement != null)
{
dtsIdDictionary.Add(dtsIdElement.Value, nameElement.Value);
dtsIdElement.Value = nameElement.Value;
}
else
{
dtsIdElement.Value = "__GUID__";
}
}
foreach (XAttribute refAttribute in (IEnumerable)document.XPathEvaluate("//@IDREF"))
{
refAttribute.Value = dtsIdDictionary[refAttribute.Value];
}
foreach (XElement connectionRef in (IEnumerable)document.XPathEvaluate("//DTS:Executable//DTS:ObjectData//ExecutePackageTask//Connection", xmlnsManager))
{
connectionRef.Value = dtsIdDictionary[connectionRef.Value];
}
foreach (XAttribute connectionRef in (IEnumerable)document.XPathEvaluate("//connection/@connectionManagerID"))
{
connectionRef.Value = dtsIdDictionary[connectionRef.Value];
}
return document;
}
示例9: GetMemberElement
private static string GetMemberElement(FunctionDescriptor function,
string suffix, XDocument xml)
{
// Construct the entire function descriptor name, including CLR style names
string clrMemberName = GetMemberElementName(function);
// match clr member name
var match = xml.XPathEvaluate(
String.Format("string(/doc/members/member[@name='{0}']/{1})", clrMemberName, suffix));
if (match is String && !string.IsNullOrEmpty((string)match))
{
return match as string;
}
// fallback, namespace qualified method name
var methodName = function.QualifiedName;
// match with fallback
match = xml.XPathEvaluate(
String.Format(
"string(/doc/members/member[contains(@name,'{0}')]/{1})", methodName, suffix));
if (match is String && !string.IsNullOrEmpty((string)match))
{
return match as string;
}
return String.Empty;
}
示例10: PrepareTemplate
protected virtual ParsedTemplate PrepareTemplate(TemplateData templateData)
{
// clone orig doc
XDocument workingDoc = new XDocument(this._templateDefinition);
// start by loading any parameters as they are needed for meta-template evaluation
Dictionary<string, XPathVariable> globalParams = new Dictionary<string, XPathVariable>();
XElement[] paramNodes = workingDoc.Root.Elements("parameter").ToArray();
foreach (XElement paramNode in paramNodes)
{
var tmplVar = new XPathVariable
{
Name = paramNode.Attribute("name").Value,
ValueExpression =
paramNode.Attribute("select") == null
? null
: paramNode.Attribute("select").Value,
};
globalParams.Add(tmplVar.Name, tmplVar);
}
CustomXsltContext customContext = new CustomXsltContext();
Func<string, object> onFailedResolve =
s =>
{
throw new InvalidOperationException(
String.Format("Parameter '{0}' could not be resolved.", s));
};
customContext.OnResolveVariable +=
s =>
{
XPathVariable var;
// if it's defined
if (globalParams.TryGetValue(s, out var))
{
// see if the user provided a value
object value;
if (templateData.Arguments.TryGetValue(s, out value))
return value;
// evaluate default value
if (!String.IsNullOrWhiteSpace(var.ValueExpression))
return workingDoc.XPathEvaluate(var.ValueExpression,
customContext);
}
return onFailedResolve(s);
};
// check for meta-template directives and expand
XElement metaNode = workingDoc.Root.Elements("meta-template").FirstOrDefault();
// we're going to need this later
XmlFileProviderResolver fileResolver = new XmlFileProviderResolver(this._fileProvider, this._basePath);
while (metaNode != null)
{
if (EvalCondition(customContext, metaNode, this.GetAttributeValueOrDefault(metaNode, "condition")))
{
#region Debug conditional
#if DEBUG
const bool debugEnabled = true;
#else
const bool debugEnabled = false;
#endif
#endregion
XslCompiledTransform metaTransform = this.LoadStylesheet(metaNode.Attribute("stylesheet").Value);
XsltArgumentList xsltArgList = new XsltArgumentList();
// TODO this is a quick fix/hack
xsltArgList.AddExtensionObject("urn:lostdoc-core", new TemplateXsltExtensions(null, null));
var metaParamNodes = metaNode.Elements("with-param");
foreach (XElement paramNode in metaParamNodes)
{
string pName = paramNode.Attribute("name").Value;
string pExpr = paramNode.Attribute("select").Value;
xsltArgList.AddParam(pName,
string.Empty,
workingDoc.XPathEvaluate(pExpr, customContext));
}
XDocument outputDoc = new XDocument();
using (XmlWriter outputWriter = outputDoc.CreateWriter())
{
metaTransform.Transform(workingDoc.CreateNavigator(),
xsltArgList,
outputWriter,
fileResolver);
//.........这里部分代码省略.........
示例11: XPathCount
private static int XPathCount(XDocument output, string path)
{
return Convert.ToInt32(output.XPathEvaluate(String.Format("count({0})", path)));
}
示例12: XPathIsValid
private bool XPathIsValid(XDocument doc, XElement element, string xPath)
{
var xPathEvaluate = doc.XPathEvaluate(xPath);
var xPathResults = doc.XPathSelectElements(xPath).ToArray();
var result = (xPathResults.Count() == 1 &&
xPathResults.First().ToString() == element.ToString());
return result;
}
示例13: GetTargetImportNode
private static XElement GetTargetImportNode(XDocument document)
{
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("aw", "http://schemas.microsoft.com/developer/msbuild/2003");
var importProjectNode =
(IEnumerable)
document.XPathEvaluate("/aw:Project/aw:Import[@Project='BridgeBuildTask.targets']",
namespaceManager);
var linqBridgeTargetImportNode = importProjectNode.Cast<XElement>().FirstOrDefault();
return linqBridgeTargetImportNode;
}
示例14: XPathExists
private static bool XPathExists(XDocument output, string path)
{
return Convert.ToBoolean(output.XPathEvaluate(String.Format("boolean({0})", path)));
}
示例15: GetTestProvider
private static string GetTestProvider(XDocument file)
{
return file.XPathEvaluate("string(/configuration/specFlow/unitTestProvider/@name)") as string;
}