Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在没有根节点的情况下解析xml文档?_C#_.net - Fatal编程技术网

C# 如何在没有根节点的情况下解析xml文档?

C# 如何在没有根节点的情况下解析xml文档?,c#,.net,C#,.net,我有一个xml文档,没有根节点。它看起来像这样: <?xml version="1.0"?> <Line> <City>Paris</City> <Country>France</Country> </Line> <Line> <City>Lissabon</City> <Countr

我有一个xml文档,没有根节点。它看起来像这样:

<?xml version="1.0"?>
    <Line>
        <City>Paris</City>
        <Country>France</Country>
    </Line>
    <Line>
        <City>Lissabon</City>
        <Country>Spain</Country>
    </Line>

巴黎
法国
里斯本
西班牙

否我想通过
Line
读取
Line
并将内容写入数据库。然而,
XmlDocument
似乎坚持必须存在根节点。如何处理此文件?

如果要将其解析为XML文档,可以添加根节点,如中建议的Denis

如果您只想读取每一行并将其写入数据库,则可以像处理普通(文本)文件一样处理该文件,并使用
StreamReader
逐行读取其内容

这看起来像这样:

<?xml version="1.0"?>
    <Line>
        <City>Paris</City>
        <Country>France</Country>
    </Line>
    <Line>
        <City>Lissabon</City>
        <Country>Spain</Country>
    </Line>
字符串行;
//读取文件并逐行处理。
var reader=新的StreamReader(文件路径);
而((line=reader.ReadLine())!=null)
{
//根据需要,可以剥离XML标记
//并将该行写入数据库
}  
reader.Close();

您可以尝试类似的方法(简单的WinForms应用程序,带有一个按钮和一个显示测试输出的富文本框):

使用系统;
使用系统文本;
使用System.Xml;
使用System.Windows.Forms;
命名空间windowsformsap11
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
StringBuilder sb=新的StringBuilder();
XmlReaderSettings设置=新的XmlReaderSettings
{
ConformanceLevel=ConformanceLevel.Fragment
};
使用(XmlReader=XmlReader.Create(@“c:\ab\countries.xml”,设置))
{
while(reader.Read())
{
if(reader.Name!=“Line”)//忽略节点
{
开关(reader.NodeType)
{
case XmlNodeType.Element:
sb.Append(string.Format(“{0}:”,reader.Name));
打破
案例XmlNodeType.Text:
Append(string.Format(“{0}{1}”,reader.Value,Environment.NewLine));
打破
}
}
}
}
richTextBox1.Text=sb.ToString();
}
}
}

可能不是最佳解决方案,但您可以从XML创建一个
列表(或数组),并插入缺少的节点:

// Read lines into List
var list = File.ReadLines("doc.xml").ToList();
// Insert missing nodes
list.Insert(1, "<root>"); // Use 1, because 0 is XML directive
list.Insert(list.Count, "</root>"); //Add closing tag to the end
// Create final XML string with LINQ
var xml_str = list.Aggregate("", (acc, s) => acc + s);
// Having a string, we can create, for instance, XElement (or XDocument)
var xml = XElement.Parse(xml_str);
Console.WriteLine(xml.Element("Line").Element("City").Value);
//Output: Paris
//将行读入列表
var list=File.ReadLines(“doc.xml”).ToList();
//插入缺少的节点
列表。插入(1“”;//使用1,因为0是XML指令
列表。插入(list.Count,“”)//将结束标记添加到末尾
//使用LINQ创建最终的XML字符串
var xml_str=list.Aggregate(“,(acc,s)=>acc+s);
//有了字符串,我们可以创建,例如,XElement(或XDocument)
var xml=XElement.Parse(xml_str);
Console.WriteLine(xml.Element(“Line”).Element(“City”).Value);
//产出:巴黎

您可以添加根元素,请参见本主题:您可以发布您尝试过的内容吗?不,对不起,使用“逐行”我指的是“行”标记。我不想写物理行。