-1

我有一个这样的xml文件

<SequenceFlow>
 <FlowWriteLine> hiiii </FlowWriteLine>
</SequenceFlow> 

我想用 C# 在 silverlight 中创建另一个 Xml 文件。XDocument 在 Silverlight 中使用。此 Xml 中的每个节点都等于另一个单词,例如

序列流=工作流,

FlowWriteLine=WriteLine

所以当我创建新的 Xml 时,它会像这样

<WorkFlow>
 <WriteLine> hiii </WriteLine>
</WorkFlow>

那么我如何使用旧的 Xml 创建新的 Xml ..请帮助我......提前谢谢..

4

1 回答 1

1

您可以简单地根据当前节点名称设置节点名称。在下面的示例中,我使用了一个字典来替换节点名称。

您可以使用您拥有的任何其他逻辑来替换节点名称,就像在原始到新的映射中一样。

XDocument doc = XDocument.Parse(@"<SequenceFlow>
    <FlowWriteLine> hiiii </FlowWriteLine>
    <NotToBeReplaced>byeee</NotToBeReplaced>
    </SequenceFlow> ");

Dictionary<string, string> replacements = new Dictionary<string, string>() { { "SequenceFlow", "Workflow" }, { "FlowWriteLine", "WriteLine" } };

foreach (XElement child in doc.Root.DescendantsAndSelf())
{
    string replacementValue = string.Empty;
    if (replacements.TryGetValue(child.Name.LocalName, out replacementValue))   
    {
        child.Name = replacementValue;
    }
}

以上给出的输出为

<Workflow>
  <WriteLine> hiiii </WriteLine>
  <NotToBeReplaced>byeee</NotToBeReplaced>
</Workflow>
于 2013-01-23T10:14:47.213 回答