如何转换List< string>到C#中的xml文档 [英] How to convert List<string> to xml document in c#
问题描述
我想在C#中将列表转换为xml文档.
I would like to convert the List to a xml document in c#.
我的代码如下:
List<string> BugWSResponseList1 = new List<string>();
Logger.Write("\n\n" + DateTime.Now + " : " + " : START : Creation of a set of Bugs via bug.Add API");
BugWSResponseList1 = CreateBugs(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile);
Logger.Write("\n\n" + DateTime.Now + " : " + " : END : Creation of a set of Bugs via bug.Add API");
我一直试图将列表响应转换为字符串,然后尝试将其转换为xml文档,但是当我解析它时,它表明xml中存在多个根元素.
I have been trying to convert the list response to string and then tried to convert it in xml document but when i parse it then it shows that there are more than one root elements in the xml.
xml的格式如下:
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<version>Xpress API</version>
<productarea>1</productarea>
<subarea></subarea>
<qe>sdawar</qe>
<duplicateId></duplicateId>
</Bug>
<Bug>
<family>TEST22</family>
<product>Dr.Watson</product>
<version>Xpress API</version>
<productarea>1</productarea>
<subarea></subarea>
<qe>sdawar</qe>
<duplicateId></duplicateId>
</Bug>
请注意,它在xml中有两个不同的节点. 到目前为止,我正在做的是将List BugWSResponseList1转换为字符串,然后将其加载为xml.这是我正在执行的代码:
Please note that it has two different nodes in the xml. What i am doing so far is converting List BugWSResponseList1 to a string and then loading it as xml.here is the code i am doing:
XmlDocument xd = new XmlDocument();
string s = string.Join(",", BugWSResponseList2);
xd.LoadXml(s);
但是当我这样做时,它表示xml中有多个根元素.
But when i am doing it it says that there are more than one root element in the xml.
我想将BugWSResponseList1列表转换为xml文档,因为我需要对其进行进一步解析以执行代码. 任何帮助,将不胜感激. 谢谢
I would like to convert the List BugWSResponseList1 to an xml document as i need to parse it further for my code execution. Any help would be appreciated. Thanks
推荐答案
您可以使用XElement或XDocument和LINQ.提供XML外观的示例,我们可以提供更多信息.
You may use XElement or XDocument and LINQ. Providing a sample of what the XML should look like we could provide more info.
例如:
BugWSResponseList1.Add("<Bug><family>TEST22</family><product>Dr.Watson</product><version>Xpress API</version><productarea>1</productarea><subarea></subarea><qe>sdawar</qe><duplicateId></duplicateId></Bug>");
BugWSResponseList1.Add("<Bug><family>ESG</family><product>Dr.Watson</product><version>Xpress API</version><productarea>1</productarea><subarea></subarea><qe>sdawar</qe><duplicateId></duplicateId></Bug>");
XElement xe = new XElement
(
"Bugs",
BugWSResponseList1
.Select
(
x=>
XElement.Parse(x)
)
);
因此,在我用两段xml加载列表之后,您提供了以下操作:
So after i have loaded the list with the two pieces of xml you have provided i do the following:
我创建一个新的XElement来保存XML的根.我将该根命名为Bugs
.然后,作为该根的子级,我将您的List的内容解析为XElement对象后放入其中.
I create a new XElement that will hold the root of the XML. I name that root as Bugs
. Then as children of that root i put the contents of your List after parsing them into XElement objects.
上面的代码将导致:
<Bugs>
<Bug>
<family>TEST22</family>
<product>Dr.Watson</product>
<version>Xpress API</version>
<productarea>1</productarea>
<subarea></subarea>
<qe>sdawar</qe>
<duplicateId></duplicateId>
</Bug>
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<version>Xpress API</version>
<productarea>1</productarea>
<subarea></subarea>
<qe>sdawar</qe>
<duplicateId></duplicateId>
</Bug>
</Bugs>
您所缺少的是根元素. XmlDocument需要定义一个根元素.
What you were missing was the root element. XmlDocument needs to have a root element defined.
在您的情况下,只需在连接的字符串的开头和结尾加上如下代码,就可以避免在代码中使用它,但是我认为我的解决方案更可靠.
In your case you could get away with it in you code by just adding in the start of your joined string and in the end, like the following, but i think my solution is more robust.
XmlDocument xd = new XmlDocument();
string s = "<Bugs>" + string.Join(",", BugWSResponseList2) + "</Bugs>";
xd.LoadXml(s);
这篇关于如何转换List< string>到C#中的xml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!