[英]How to get Guid Id of CustomXMLPart in OpenXML
I have a program that processes some documents and based on business rules it creates CustomXMLParts and creates ContentControl for each part. 我有一个处理一些文档的程序,它根据业务规则创建CustomXMLParts并为每个零件创建ContentControl。 This is the function that creates the CustomXMlPart with OpenXML and adds a
Guid
as an attribute. 这是使用OpenXML创建CustomXMlPart并添加
Guid
作为属性的函数。 This Guid
is used to link to a ContentControl. 该
Guid
用于链接到ContentControl。
private static void InsertMetadata(WordprocessingDocument document, Dictionary<string, object> metaData, string customPartName, string id = null)
{
XElement metadataElement = null;
CustomXmlPart customXml = null;
if (id != null)
{
metadataElement = new XElement(customPartName, new XAttribute("Guid", "NeedsID"));
customXml = document.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
var guidID = document.MainDocumentPart.GetIdOfPart(customXml);
}
else
{
metadataElement = new XElement(customPartName);
customXml = document.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
}
foreach (KeyValuePair<string, object> entry in metaData)
{
metadataElement.Add(new XElement(entry.Key, entry.Value));
}
using (StreamWriter sr = new StreamWriter(customXml.GetStream()))
{
sr.Write(metadataElement);
}
}
There is a WordAddIn That reads the parts like this and sets the Guid
attribute to the ID of the customXML element 有一个WordAddIn可以读取类似的部分,并将
Guid
属性设置为customXML元素的ID。
foreach (CustomXMLPart part in userAction.Application.ActiveDocument.CustomXMLParts.OfType<CustomXMLPart>())
{
var section = part.SelectSingleNode("MySection");
if (section!=null)
{
part.SetAttributeValue("Guid", part.Id.ToString());
}
}
In the WordAddIn the CustomXMLPart has the property Id and it is a Guid. 在WordAddIn中,CustomXMLPart具有属性Id,它是一个Guid。 The AddIn reads the parts and updates the attribute with the Guid that is the Id of the part.
AddIn读取零件并使用Guid更新属性,该Guid是零件的ID。
The issue i'm having is that I can create the XML parts no problem and i can Create the ContentControl and add the tag of the Guid so the CC and XMLpart are linked. 我遇到的问题是我可以毫无问题地创建XML部件,也可以创建ContentControl并添加Guid的标记,以便CC和XMLpart链接在一起。 I create the CustomXMLPart then the CC and i Add the guid to link.
我先创建CustomXMLPart,然后创建CC,然后添加guid进行链接。 However, when using openXML i cant get the ID as a guid after I've created and added it to the DocumentPart
但是,当使用openXML时,在创建并将其添加到DocumentPart后,我无法将其ID用作GUID
var guidID = document.MainDocumentPart.GetIdOfPart(customXml);
This does not return a Guid. 这不会返回Guid。 It returns something like this Rd96388a5e95041a0
它会返回类似Rd96388a5e95041a0的内容
But i need a Guid so i can add it as a tag to the CC. 但是我需要一个Guid,以便可以将其作为标签添加到CC。 I couldn't figure a way to add a CustomXMLPart with a predefined Guid so i just create the part then try to get the Id.
我无法找到一种添加带有预定义的Guid的CustomXMLPart的方法,所以我只创建该部分然后尝试获取ID。
First, AddCustomXmlPart
has a constructor which accepts id: MainDocumentPart.AddCustomXmlPart method (CustomXmlPartType, String) as a string. 首先,
AddCustomXmlPart
具有一个接受ID的构造函数: MainDocumentPart.AddCustomXmlPart方法(CustomXmlPartType,String)作为字符串。 So you could do something like: 因此,您可以执行以下操作:
var customXml = doc.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml, Guid.NewGuid().ToString());
WARNING! 警告!
Seems logical, but wait. 似乎合乎逻辑,但请稍等。 Think what are you really trying to do here?
想想您在这里真正想做什么? You are trying to assign
Guid
as an xsd:ID
! 您正在尝试将
Guid
分配为xsd:ID
! In case if you are not aware 如果您不知道
The type xsd:ID is used for an attribute that uniquely identifies an element in an XML document.
xsd:ID类型用于唯一标识XML文档中元素的属性。 An xsd:ID value must be an NCName.
xsd:ID值必须是NCName。 This means that it must start with a letter or underscore, and can only contain letters, digits, underscores, hyphens, and periods.
这意味着它必须以字母或下划线开头,并且只能包含字母,数字,下划线,连字符和句点。
Do all Guids
start with a letter? 所有
Guids
以字母开头吗? NO! 没有! So the above line will work at times and will throw an exception otherwise.
因此,以上行有时会起作用,否则会引发异常。 So the question is do you really need
Guids
? 所以问题是您真的需要
Guids
吗?
If you still are persistent to use Guids
then append each new guid with a char at the beginning and be careful to remove it while parsing the string id to Guid. 如果仍然坚持使用
Guids
每个新guid的开头添加一个字符,并在将字符串id解析为Guid时小心将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.