你会如何围绕<div class="overflow"></div>节点周围的所有表格?这显然不会这样做:
if (oldElement.Name == "table")
{
HtmlDocument doc = new HtmlDocument();
HtmlNode newElement = doc.CreateElement("div");
newElement.SetAttributeValue("class", "overflow");
newElement.AppendChild(oldElement);
oldElement.ParentNode.ReplaceChild(newElement, oldElement);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试该代码时,表中没有任何反应.但如果我使用:
if (oldElement.Name == "table")
{
oldElement.Remove();
}
Run Code Online (Sandbox Code Playgroud)
确实删除了所有表,所以我确定我正在访问正确的节点.
这是我的(简化)models.py:
class MyList(models.Model):
title = models.CharField(max_length=40)
participants = models.ManyToManyField(
settings.AUTH_USER_MODEL,
through='ParticipantsInList',
related_name='participants',
)
class ParticipantsInList(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='user')
list_parent = models.ForeignKey(MyList, related_name='list_parent')
moderator = models.BooleanField(default=False)
Run Code Online (Sandbox Code Playgroud)
和我的serializers.py:
class ParticipantsInListSerializer(serializers.ModelSerializer):
class Meta:
model = ParticipantsInList
exclude = ('id',)
Run Code Online (Sandbox Code Playgroud)
和我的views.py:
class ParticipantsInListView(generics.ListCreateAPIView):
serializer_class = ParticipantsInListSerializer
def get_queryset(self):
list_id = self.kwargs['list_pk']
# This works:
# return ParticipantsInList.objects.filter(list_parent=list_id)
# While this doesn't:
return MyList.objects.get(pk=list_id).participants.all()
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么在views.py中使用它:ParticipantsInList.objects.filter(list_id = list_id)工作,而使用List.objects.get(pk = list_id).participants.all()引发异常'RelatedManager'对象没有属性'pk'.
我想使用后者,因为我觉得它更好看,也因为我相信它应该工作..
Form1使用Form2.ShowDialog()打开Form2.在Form2中打开一个文件对话框.当文件浏览器关闭时,Form2也会关闭(因为我认为会激活DialogResult)
我似乎无法通过搜索找到解决方案,也许是因为我不确切知道要搜索什么.那么在没有Form2关闭的情况下实现这一目标的首选方法是什么?
删除所有空节点和不需要节点的首选方法是什么?例如
<p></p>应该删除,<font><p><span><br></span></p></font>也应该删除(因此在这种情况下br标签被认为是不必要的)
我是否必须使用某种递归函数?我正在思考这个问题:
RemoveEmptyNodes(HtmlNode containerNode)
{
var nodes = containerNode.DescendantsAndSelf().ToList();
if (nodes != null)
{
foreach (HtmlNode node in nodes)
{
if (node.InnerText == null || node.InnerText == "")
{
RemoveEmptyNodes(node.ParentNode);
node.Remove();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这显然不起作用(stackoverflow异常).