Linq到XML,如何访问C#中的元素?

Linq到XML,如何访问C#中的元素?,c#,linq-to-xml,C#,Linq To Xml,以下是我需要解析的XML: <root> <photo>/filesphoto.jpg</photo> <photo:mtime>12</photo:mtime> <text>some text</text> </root> 我怎样才能访问?我的朋友,这是一种非法的xml格式 你不能有冒号,它是非法的xml格式,我的朋友 不能有冒号元素mti

以下是我需要解析的XML:

 <root>
         <photo>/filesphoto.jpg</photo>
         <photo:mtime>12</photo:mtime>
         <text>some text</text>
 </root>

我怎样才能访问

我的朋友,这是一种非法的xml格式
你不能有冒号,它是非法的xml格式,我的朋友
不能有冒号元素
mtime
位于映射到
photo
的命名空间中。您可以按如下方式访问它:

var doc = XDocument.Parse(xml.Text);
XNamespace ns = "your nanespace URI goes here"
doc.Descendants(ns + "mtime").FirstOrDefault().Value;
但是,如果没有namepsace映射,则XML文档无效。我希望它看起来像这样:

 <root xmlns:photo="your nanespace URI goes here">
         <photo>/filesphoto.jpg</photo>
         <photo:mtime>12</photo:mtime>
         <text>some text</text>
 </root>

/filesphoto.jpg
12
一些文本

元素
mtime
位于映射到
photo
的命名空间中。您可以按如下方式访问它:

var doc = XDocument.Parse(xml.Text);
XNamespace ns = "your nanespace URI goes here"
doc.Descendants(ns + "mtime").FirstOrDefault().Value;
但是,如果没有namepsace映射,则XML文档无效。我希望它看起来像这样:

 <root xmlns:photo="your nanespace URI goes here">
         <photo>/filesphoto.jpg</photo>
         <photo:mtime>12</photo:mtime>
         <text>some text</text>
 </root>

/filesphoto.jpg
12
一些文本
答案就在这里 谢谢你 将具有名称空间的xml片段解析为XElement:

public static XElement parseWithNamespaces(String xml, String[] namespaces) {
    XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(new NameTable());
    foreach (String ns in namespaces) { nameSpaceManager.AddNamespace(ns, ns); }
    return XElement.Load(new XmlTextReader(xml, XmlNodeType.Element, 
        new XmlParserContext(null, nameSpaceManager, null, XmlSpace.None)));
}
使用您的精确输入:

string xml = 
"<root>
    <photo>/filesphoto.jpg</photo>
    <photo:mtime>12</photo:mtime>
    <text>some text</text>
</root>";

XElement x = parseWithNamespaces(xml, new string[] { "photo" });
foreach (XElement e in x.Elements()) { 
    Console.WriteLine("{0} = {1}", e.Name, e.Value); 
}
Console.WriteLine(x.Element("{photo}mtime").Value);
答案就在这里 谢谢你 将具有名称空间的xml片段解析为XElement:

public static XElement parseWithNamespaces(String xml, String[] namespaces) {
    XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(new NameTable());
    foreach (String ns in namespaces) { nameSpaceManager.AddNamespace(ns, ns); }
    return XElement.Load(new XmlTextReader(xml, XmlNodeType.Element, 
        new XmlParserContext(null, nameSpaceManager, null, XmlSpace.None)));
}
使用您的精确输入:

string xml = 
"<root>
    <photo>/filesphoto.jpg</photo>
    <photo:mtime>12</photo:mtime>
    <text>some text</text>
</root>";

XElement x = parseWithNamespaces(xml, new string[] { "photo" });
foreach (XElement e in x.Elements()) { 
    Console.WriteLine("{0} = {1}", e.Name, e.Value); 
}
Console.WriteLine(x.Element("{photo}mtime").Value);


当你尝试类似于文本时发生了什么?System.Exception{System.Xml.xmleexception}{“名称中不能包含“:”字符,十六进制值0x3A。”}只是想知道,
Parse
对你有用吗?我想它应该失败了?哎呀,这就是为什么我不能访问它,你是对的,“Parse”没有失败,但是在解析的xml中没有“12”。任何想法,如何解决它?也许你正在寻找这个当你尝试它类似于文本时发生了什么?System.Exception{System.Xml.xmleexception}{“名称中不能包含“:”字符,十六进制值0x3A。”}只是想知道,
Parse
对你有用吗,我想它应该失败了?哦,这就是为什么我不能访问它的原因,你说得对,“解析”并没有失败,但解析后的xml中没有“12”。有什么想法,怎么解决吗?也许你在寻找答案,你的答案与问题不符,也许它已经更新了?当xml被写入时,它与colinnew匹配什么是“列”?格式良好的XML文档规范中没有关于列的内容。@ColineE“如果没有名称空间映射,您的XML文档无效”:没有名称空间,字符是非法的,这就是XML的命名方式。。。你是说冒号,不是列。现在我明白了。谢谢您提供的信息。您的答案与问题不匹配,可能是因为它已经更新了?在编写xml时,它与ColinNewWhat是“列”匹配?格式良好的XML文档规范中没有关于列的内容。@ColineE“如果没有名称空间映射,您的XML文档无效”:没有名称空间,字符是非法的,这就是XML的命名方式。。。你是说冒号,不是列。现在我明白了。谢谢你的信息。好的,我需要更多的帮助。我的xml不正确,如何更正它?我试图插入一些字符串,但还有一个问题是如何向字符串中添加(“)双引号字符?
xml.Text.insert(5,“xmlns:photo=http://www.w3.org”;
我也尝试过这个解决方案[链接],仍然无法理解。好的,我需要更多帮助。我的xml不正确,如何更正?我已尝试插入一些字符串,但如何在字符串中添加(“)双引号字符还有另一个问题<代码>xml.Text.Insert(5,“xmlns:photo=http://www.w3.org");我也尝试过这个解决方案[link],但还是不明白。