通过c#操作xml
我有以下 xml 结构我想通过 c# 更新它 如何更新 是否可以通过 Linq 进行更新,如果可以,那么如何进行? 我想通过代码添加 UnitTest、TestList、TestEntry 和 UnitTestResults 元素。
`
<?xml version="1.0" encoding="UTF-8"?>
<TestRun id="1" xmlns="http://microsoft.com/schemas">
<TestDefinitions>
<UnitTest name="Test1" id="T1">
<Execution id="E1" />
</UnitTest>
<UnitTest name="Test2" id="T2">
<Execution id="E2" />
</UnitTest>
:
:
</TestDefinitions>
<TestLists>
<TestList name="List1" id="L1" />
<TestList name="List2" id="L2" />
:
:
</TestLists>
<TestEntries>
<TestEntry testId="T1" executionId="E1" testListId="L1" />
<TestEntry testId="T2" executionId="E2" testListId="L2" />
:
:
</TestEntries>
<Results>
<UnitTestResult executionId="E1" testId="T1" testName="Test1" >
<Output>
<ErrorInfo>
<Message>Hi</Message>
</ErrorInfo>
</Output>
</UnitTestResult>
</Results>
<Results>
:
:
</TestRun>
`
I have following xml structure I want to update it through c#
How to update it
Is it possible to update through Linq if yes then how to do it?
I want to add UnitTest, TestList, TestEntry and UnitTestResults elements through code.
`
<?xml version="1.0" encoding="UTF-8"?>
<TestRun id="1" xmlns="http://microsoft.com/schemas">
<TestDefinitions>
<UnitTest name="Test1" id="T1">
<Execution id="E1" />
</UnitTest>
<UnitTest name="Test2" id="T2">
<Execution id="E2" />
</UnitTest>
:
:
</TestDefinitions>
<TestLists>
<TestList name="List1" id="L1" />
<TestList name="List2" id="L2" />
:
:
</TestLists>
<TestEntries>
<TestEntry testId="T1" executionId="E1" testListId="L1" />
<TestEntry testId="T2" executionId="E2" testListId="L2" />
:
:
</TestEntries>
<Results>
<UnitTestResult executionId="E1" testId="T1" testName="Test1" >
<Output>
<ErrorInfo>
<Message>Hi</Message>
</ErrorInfo>
</Output>
</UnitTestResult>
</Results>
<Results>
:
:
</TestRun>
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,通过 LINQ 可以实现。这是添加新
UnitTest
元素的示例代码:要添加该元素,您必须创建
XElement
对象并将新元素的名称及其所有内容传递给其构造函数像子元素、属性(通过逗号)。然后,您必须指定要添加新元素的位置:通过从根元素到 XML 三个元素(如本例所示)或通过查询。您可以通过 LINQ 查询找到所需的元素。下一个示例演示如何从
TestList
中获取 idL1
的所有TestEntries
:此查询的结果对象具有带有有用属性的匿名类型。如果您想使用
XElement
对象,只需将select new ...
更改为select e
即可。如果您想更新元素的值,请找到它(参见上文)并调用
SetValue()
方法。如果您使用命名空间(如您的文件),则必须创建具有所需值的
XNamespace
对象,并将其与您需要的所有元素名称连接起来。要将更改保存到 dist 上的 xml 文件,请调用
Save()
方法。MSDN 中的 LINQ to XML 概述
Yes, it is possible by LINQ. This is the example code to add new
UnitTest
element:To add the element you must create the
XElement
object and pass to its constructor the name of the new element and all its stuff like child elements, attributes (through a comma). Then you must specify where you want to add new element: by going from the Root element through the XML three (like in this example) or by query.You can find needed element by LINQ query. Next example shows how to get all
TestEntries
from theTestList
with idL1
:Result objects from this query have anonymous type with useful properties. If you want work with
XElement
objects, simply changeselect new ...
toselect e
.If you want update the value of the element, find it (look above) and call the
SetValue()
method.If you working with namespaces (like you file) you must create
XNamespace
object with needed value and concatenate it with all elements' names that you need.To save your changes to the xml file on dist, call
Save()
method.LINQ to XML Overview in MSDN