简体   繁体   English

比较OpenXML中的两个XML元素

[英]Comparing Two XML Elements In OpenXML

I'm new to XML and C# so I'm having a little trouble implementing the following. 我是XML和C#的新手,因此在实现以下内容时遇到了一些麻烦。 Hopefully someone can point me in the right direction. 希望有人可以指出正确的方向。 So I'm developing in C# with OpenXML 2.5 and I'm trying to check, after I create a Font, if that Font already exists in the Font collection. 因此,我正在使用带有OpenXML 2.5的C#语言进行开发,并且在创建字体后尝试检查Font集合中是否已存在该Font。 If it already does then I want to return the index of the Font. 如果已经这样做了,那么我想返回字体的索引。

I am stuck on writing an efficient method to compare these Font objects and I believe that comparing their respective XML code is the way to go. 我一直坚持写一种比较这些Font对象的有效方法,并且我相信比较它们各自的XML代码是正确的方法。 I believe that these Font objects are wrappers for XML code. 我相信这些Font对象是XML代码的包装器。 So I thought that I should be able to compare two XML elements and figure out if the Font already exists or not. 因此,我认为我应该能够比较两个XML元素并确定Font是否已经存在。

Does this make any sense? 这有道理吗? Here is an example, because I fear that I'm being overly complicated in my explanation. 这是一个例子,因为我担心我的解释过于复杂。

1. Basically, I want to find if this: 1.基本上,我想看看是否:

  <x:font>
    <x:b />
    <x:sz val="18" />
    <x:color theme="3" />
    <x:name val="Cambria" />
    <x:family val="2" />
    <x:scheme val="major" />
  </x:font>

2. Already exists in here: 2.在这里已经存在:

<x:fonts count="18" x14ac:knownFonts="1" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <x:font>
    <x:sz val="11" />
    <x:color theme="1" />
    <x:name val="Calibri" />
    <x:family val="2" />
    <x:scheme val="minor" />
  </x:font>
  <x:font>
    <x:sz val="11" />
    <x:color theme="1" />
    <x:name val="Calibri" />
    <x:family val="2" />
    <x:scheme val="minor" />
  </x:font>
  <x:font>
    <x:b />
    <x:sz val="18" />
    <x:color theme="3" />
    <x:name val="Cambria" />
    <x:family val="2" />
    <x:scheme val="major" />
  </x:font>
  <x:font>
    <x:b />
    <x:sz val="15" />
    <x:color theme="3" />
    <x:name val="Calibri" />
    <x:family val="2" />
    <x:scheme val="minor" />
  </x:font>
</x:fonts>

3. And the method returns the index of the Font. 3.然后该方法返回Font的索引。 So in this example my function would return 2 . 因此,在此示例中,我的函数将返回2

Any help with this would be greatly appreciated! 任何帮助,将不胜感激!

thanks, Justin 谢谢,贾斯汀

Check out XPath for querying XMLDocuments, and then you can use FindNode() on the XMLDocument with the XPath query. 签出XPath以查询XMLDocuments,然后可以将XMLDocument上的FindNode()与XPath查询一起使用。

However, what it won't do is return the Index Number. 但是,它不会返回索引号。 Unless otherwise directed, XML nodes are not ordered and so rthe idea of saying "it's the third" is not done because next time you look it might be the fifth! 除非另有指示,否则XML节点不会排序,因此不会说“它是第三个”,因为下次您看它可能是第五个! (Probably not, but behavior like that would be within the spec of XML). (可能不是,但是这样的行为在XML规范之内)。 What you can do, though, is get the actual node () from which you can get the parent () node that contains it. 但是,您可以做的是获取实际的node(),从中可以获取包含该节点的parent()节点。

Appreciate the help, but figured out an easier way of doing it actually from within the sdk. 感谢帮助,但实际上从sdk中找到了一种更简单的方法。 Turns out there is a method called OuterXml for each OpenXml Object. 事实证明,每个OpenXml对象都有一个称为OuterXml的方法。

According to the Microsoft definition: OuterXml: Gets the markup that represents the current element and all of its child elements. 根据Microsoft的定义: OuterXml: Gets the markup that represents the current element and all of its child elements.

It is much better than InnerXml: Gets or sets the markup that represents only the child elements of the current element. 它比InnerXml: Gets or sets the markup that represents only the child elements of the current element.更好InnerXml: Gets or sets the markup that represents only the child elements of the current element.

So I can simply do: 所以我可以简单地做:

private Stylesheet _stylesheet = _workbookPart.WorkbookStylesPart.Stylesheet;

public int GetFontIndex(Font font)
{
     int index = 0;
     foreach (var existingFont in _stylesheet.Descendants<Font>())
     {
         if (font.OuterXml.Equals(existingFont.OuterXml)) return index;
         index++;
     }

     return -1;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM