如何使用此内容使用C#生成XML [英] How to generate XML using C# with this content

查看:66
本文介绍了如何使用此内容使用C#生成XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我想用C#生成XML。我知道XmlDocument,XElement,XmlWriter,但无法完成以下任务。

1.我有一个包含此内容的.xml文件

< pre lang =xml> < WorkflowService

xmlns = http://schemas.microsoft .com / netfx / 2009 / xaml / servicemodel

xmlns:p1 = http://schemas.microsoft.com/netfx/2009/xaml/activities

xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml\">
< / WorkflowService >





2.我需要在此文档中添加以下内容



 <   p1:Sequence.Variables  >  
< p1:变量 < span class =code-attribute> x:TypeArguments = sometype1 名称 = name1 / >
< p1:变量 x:TypeArguments = sometype2 名称 = name2 / >
< / p1:Sequence .Variables >





3.结果如此应该是这样的



 <   WorkflowService     

xmlns = http://schemas.microsoft.com/netfx/2009/xaml/servicemodel

xmlns:p1 = http://schemas.microsoft.com/netfx/2009/xaml/activities

xmlns:x = http://schemas.microsoft .com / winfx / 2006 / xaml >

< p1:Sequence.Variables >
< p1:变量 x:TypeArguments = sometype1 名称 = name1 / >
< p1:变量 x:TypeArguments = sometype2 名称 = name2 / >
< / p1:Sequence.Variables >

< ; / WorkflowService >





你能告诉我工作代码示例(我对命名空间有些困惑,无法执行我想要的工作)。谢谢

解决方案

看看这里:

如何:控制命名空间前缀(C#)(LINQ to XML) [ ^ ]

如何:使用命名空间创建文档(C#)(LINQ to XML) [ ^ ]


Hi. I want to generate XML using C#. I know about XmlDocument, XElement, XmlWriter, but can't accomplish the following task with any of them.
1. I have an .xml file with this content

<WorkflowService 

 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/servicemodel"

 xmlns:p1="http://schemas.microsoft.com/netfx/2009/xaml/activities"

 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</WorkflowService>



2. I need to add to this document the following content

<p1:Sequence.Variables>
      <p1:Variable x:TypeArguments="sometype1" Name="name1" />
      <p1:Variable x:TypeArguments="sometype2" Name="name2" />
    </p1:Sequence.Variables>



3. So the result should be like this

<WorkflowService 

 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/servicemodel"

 xmlns:p1="http://schemas.microsoft.com/netfx/2009/xaml/activities"

 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <p1:Sequence.Variables>
      <p1:Variable x:TypeArguments="sometype1" Name="name1" />
      <p1:Variable x:TypeArguments="sometype2" Name="name2" />
    </p1:Sequence.Variables>

</WorkflowService>



Can you please show me the working code sample (i have some confusion regarding namespaces and can't perform what i want). Thanks

解决方案

Have a look here:
How to: Control Namespace Prefixes (C#) (LINQ to XML)[^]
How to: Create a Document with Namespaces (C#) (LINQ to XML)[^]


这篇关于如何使用此内容使用C#生成XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆