本文整理汇总了C#中System.Xml.Linq.XNode.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# XNode.GetType方法的具体用法?C# XNode.GetType怎么用?C# XNode.GetType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XNode
的用法示例。
在下文中一共展示了XNode.GetType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetStringValue
public static String GetStringValue(XNode item)
{
if(item == null)
throw new ArgumentNullException("item");
if (item is XElement)
return ((XElement)item).Value;
else
throw new Exception(string.Format("Cannot handle '{0}'.", item.GetType()));
}
示例2: WriteNode
protected void WriteNode(XNode node, TextWriter writer, DocItemWriterContext context)
{
if (node is XText || node is XCData)
{
WriteHyperlinkEnabledText(node.ToString(), writer, context);
return;
}
XElement element = node as XElement;
if (element != null)
{
switch (element.Name.ToString())
{
case "see":
{
string cref = element.Attribute("cref").Value;
string text = element.Value;
WriteHyperlinkSymbol(cref, text, writer, context);
}
return;
}
}
throw new ArgumentException(string.Format("[{0}]: Don't know how to print {1}.", this.AssociatedDocItem.Name, node.GetType().Name));
}
示例3: tvXmlDocument_SelectedItemChanged
private void tvXmlDocument_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
var selectedItem = (TreeViewItemXNode)tvXmlDocument.SelectedItem;
txNodeName.TextChanged -= new TextChangedEventHandler(txNodeName_TextChanged);
txNodeName.GotFocus -= new RoutedEventHandler(txNodeName_GotFocus);
txNodeValue.TextChanged -= new TextChangedEventHandler(txNodeValue_TextChanged);
if (selectedItem == null) {
grdNodeDetails.Visibility = Visibility.Hidden;
mSelectedItem = null;
mSelectedXNode = null;
} else {
grdNodeDetails.Visibility = Visibility.Visible;
/*if (selectedItem.EndingItem != null || (selectedItem.BeginItem == null && selectedItem.EndingItem == null)) {
//This is item is an XElement because it either:
// has ending item, which only XElement items can have; OR
// doesn't have either a begin item or an end item, which only XElement items cannot have both.
//Do nothing, just let execution fall through.
} else*/ if (selectedItem.BeginItem != null) {
//The real item is the one at the beginning.
selectedItem = selectedItem.BeginItem;
}
if (selectedItem != mSelectedItem) {
//Selected item is different from the previously selected one.
mSelectedItem = selectedItem;
mSelectedXNode = mSelectedItem.XNode;
mAttributeRows.Clear();
grdAttributes.Children.Clear();
grdAttributes.RowDefinitions.Clear();
if (typeof(XComment).IsAssignableFrom(mSelectedXNode.GetType())) {
//User selected a comment. Only its value can be changed.
var comment = (XComment)mSelectedXNode;
lbNodeType.Text = "This is a comment node. Only its value can be changed.";
grpAttributes.Visibility = Visibility.Hidden;
txNodeValue.Text = comment.Value;
txNodeName.IsEnabled = false;
txNodeName.Text = "Comment Node";
txNodeName.TextChanged -= new TextChangedEventHandler(txNodeName_TextChanged);
txNodeName.GotFocus -= new RoutedEventHandler(txNodeName_GotFocus);
txNodeValue.TextChanged += new TextChangedEventHandler(txNodeValue_TextChanged);
} else if (typeof(XElement).IsAssignableFrom(mSelectedXNode.GetType())) {
var element = (XElement)mSelectedXNode;
XAttributeGridRow newRow;
lbNodeType.Text = "This is a normal node. You can change its name, value, and attributes.";
txNodeName.IsEnabled = true;
txNodeName.Text = element.Name.LocalName;
txNodeValue.Text = (string)mSelectedItem.ValueItem.Header;
grpAttributes.Visibility = Visibility.Visible;
if (element.HasAttributes) {
using (var attributesEnumerator = element.Attributes().GetEnumerator()) {
while (attributesEnumerator.MoveNext()) {
newRow = new XAttributeGridRow();
newRow.XAttribute = attributesEnumerator.Current;
newRow.AddToGrid(grdAttributes);
newRow.XAttributeValueChanged += new EventHandler(this.XAttribute_ValueChanged);
mAttributeRows.Add(newRow);
}
}
}
txNodeName.TextChanged += new TextChangedEventHandler(txNodeName_TextChanged);
txNodeName.GotFocus += new RoutedEventHandler(txNodeName_GotFocus);
txNodeValue.TextChanged += new TextChangedEventHandler(txNodeValue_TextChanged);
}
} else if (typeof(XComment).IsAssignableFrom(mSelectedXNode.GetType())) {
txNodeName.TextChanged -= new TextChangedEventHandler(txNodeName_TextChanged);
txNodeName.GotFocus -= new RoutedEventHandler(txNodeName_GotFocus);
txNodeValue.TextChanged += new TextChangedEventHandler(txNodeValue_TextChanged);
} else if (typeof(XElement).IsAssignableFrom(mSelectedXNode.GetType())) {
txNodeName.TextChanged += new TextChangedEventHandler(txNodeName_TextChanged);
txNodeName.GotFocus += new RoutedEventHandler(txNodeName_GotFocus);
txNodeValue.TextChanged += new TextChangedEventHandler(txNodeValue_TextChanged);
}
}
}
示例4: ShowXNode
private void ShowXNode(XNode xnode, TreeViewItemXNode parentNode, int position = -1)
{
var newItem = new TreeViewItemXNode();
newItem.XNode = xnode;
if (typeof(XComment).IsAssignableFrom(xnode.GetType())) {
var comment = (XComment)xnode;
newItem.Header = comment.Value;
} else if (typeof(XElement).IsAssignableFrom(xnode.GetType())) {
var element = (XElement)xnode;
//TreeViewItem to close opening tag.
newItem.EndingItem = new TreeViewItemXNode();
newItem.EndingItem.XNode = element;
newItem.EndingItem.BeginItem = newItem;
newItem.EndingItem.Header = "</" + element.Name + ">";
if (element.HasElements) {
var elementsEnumerator = element.Nodes().GetEnumerator();
while (elementsEnumerator.MoveNext()) {
this.ShowXNode(elementsEnumerator.Current, newItem);
}
} else if (element.Value.Length > 0) {
//TreeViewItem to show element value.
//When an XElement has a value, it has an XText node, which is in the first position.
newItem.ValueItem = new TreeViewItemXNode();
newItem.ValueItem.BeginItem = newItem;
newItem.ValueItem.XNode = element.FirstNode;
newItem.ValueItem.Header = element.Value;
newItem.Items.Add(newItem.ValueItem);
}
if (newItem.ValueItem == null) {
//Create an XNode to hold the element's value.
var text = new XText("");
element.AddFirst(text);
//TreeViewItem to show element value.
newItem.ValueItem = new TreeViewItemXNode();
newItem.ValueItem.BeginItem = newItem;
newItem.ValueItem.XNode = text;
newItem.ValueItem.Header = "";
}
this.RefreshXElementItemHeader(newItem);
} else if (typeof(XText).IsAssignableFrom(xnode.GetType())) {
var text = (XText)xnode;
newItem.BeginItem = parentNode;
newItem.Header = text.Value;
parentNode.ValueItem = newItem;
} else {
newItem.Header = "UNKNOWN XNODE!!!";
}
if (parentNode == null)
tvXmlDocument.Items.Add(newItem);
else {
if (position == -1)
parentNode.Items.Add(newItem);
else
parentNode.Items.Insert(position, newItem);
}
if (newItem.HasItems) {
newItem.Collapsed += new RoutedEventHandler(ItemCollapsed);
newItem.Expanded += new RoutedEventHandler(ItemExpanded);
}
}
示例5: GetInnerText
internal string GetInnerText(XNode node, bool dontNormalizeSpaces)
{
if (node == null)
{
throw new ArgumentNullException("node");
}
string result;
if (node is XElement)
{
result = ((XElement)node).Value;
}
else if (node is XText)
{
result = ((XText)node).Value;
}
else
{
throw new NotSupportedException(string.Format("Nodes of type '{0}' are not supported.", node.GetType()));
}
result = (result ?? "").Trim();
if (!dontNormalizeSpaces)
{
return _NormalizeSpacesRegex.Replace(result, " ");
}
return result;
}
示例6: DeepEquals
/// <summary>
/// Compares two nodes using the indicated comparison options.
/// </summary>
/// <param name="n1">The first node to compare.</param>
/// <param name="n2">The second node to compare.</param>
/// <param name="options">The options to use in the comparison.</param>
/// <returns>true if the nodes are equal, false otherwise.</returns>
public static bool DeepEquals(this XNode n1, XNode n2, ComparisonOptions options)
{
if ((n1 ?? n2) == null)
return true;
if ((n1 == null) || (n2 == null))
return false; // They are not both null, so if either is, then the other isn't
if ((n1 is XElement) && (n2 is XElement))
return DeepEquals((XElement)n1, (XElement)n2, options);
else if ((n1 is XComment) && (n2 is XComment))
return DeepEquals((XComment)n1, (XComment)n2, options);
else if ((n1 is XText) && (n2 is XText))
return DeepEquals((XText)n1, (XText)n2, options);
else if ((n1 is XProcessingInstruction) && (n2 is XProcessingInstruction))
return DeepEquals((XProcessingInstruction)n1, (XProcessingInstruction)n2, options);
else if (!(n1.GetType().IsAssignableFrom(n2.GetType()) || n2.GetType().IsAssignableFrom(n1.GetType())))
return false;
else
throw new NotImplementedException();
}