本文整理汇总了C#中XDoc.ReplaceWithNodes方法的典型用法代码示例。如果您正苦于以下问题:C# XDoc.ReplaceWithNodes方法的具体用法?C# XDoc.ReplaceWithNodes怎么用?C# XDoc.ReplaceWithNodes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类XDoc
的用法示例。
在下文中一共展示了XDoc.ReplaceWithNodes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Enforce
private void Enforce(XDoc doc, bool removeIllegalElements, bool isRoot)
{
if(doc.IsEmpty) {
return;
}
// process child elements
List<XDoc> list = doc.Elements.ToList();
foreach(XDoc child in list) {
Enforce(child, removeIllegalElements, false);
}
// skip processing of root element
if(!isRoot) {
// process element
Element elementRule;
if(!Elements.TryGetValue(doc.Name, out elementRule)) {
// element is not valid; determine what to do with it
if(removeIllegalElements) {
// replace it with its contents
doc.ReplaceWithNodes(doc);
} else {
StringBuilder attributes = new StringBuilder();
foreach(XmlAttribute attribute in doc.AsXmlNode.Attributes) {
attributes.Append(" ");
attributes.Append(attribute.OuterXml);
}
// replace it with text version of itself
if(doc.AsXmlNode.ChildNodes.Count == 0) {
doc.AddBefore("<" + doc.Name + attributes.ToString() + "/>");
doc.Remove();
} else {
doc.AddBefore("<" + doc.Name + attributes.ToString() + ">");
doc.AddAfter("</" + doc.Name + ">");
doc.ReplaceWithNodes(doc);
}
}
return;
} else if(doc.Name != elementRule.Name) {
// element has an obsolete name, substitute it
doc.Rename(elementRule.Name);
}
// process attributes
List<XmlAttribute> attributeList = new List<XmlAttribute>();
foreach(XmlAttribute attribute in doc.AsXmlNode.Attributes) {
attributeList.Add(attribute);
}
// remove unsupported attributes
if(!elementRule.Attributes.ContainsKey("*")) {
foreach(XmlAttribute attribute in attributeList) {
Attribute attributeRule;
elementRule.Attributes.TryGetValue(attribute.Name, out attributeRule);
if((attributeRule == null) && (_wildcardElement != null)) {
_wildcardElement.Attributes.TryGetValue(attribute.Name, out attributeRule);
}
if((attributeRule == null) || ((attributeRule.LegalValues.Length > 0) && (Array.BinarySearch<string>(attributeRule.LegalValues, attribute.Value) < 0))) {
doc.RemoveAttr(attribute.Name);
}
}
}
// add default attributes
foreach(Attribute attributeRule in elementRule.Attributes.Values) {
if((attributeRule.DefaultValue != null) && (doc.AsXmlNode.Attributes[attributeRule.Name] == null)) {
doc.Attr(attributeRule.Name, attributeRule.DefaultValue);
}
}
// process empty element
if(list.Count == 0) {
// check if the contents are empty
string contents = doc.Contents;
if((contents.Trim().Length == 0) && (contents.IndexOf('\u00A0') < 0)) {
switch(elementRule.Mode) {
case ProcessingMode.PadEmpty:
// add ' '
doc.ReplaceValue("\u00A0");
break;
case ProcessingMode.RemoveEmpty:
doc.Remove();
break;
}
}
}
}
}