XML的一部分:
<text:p>text1 <text:span>text2</text:span> <text:span>text3</text:span>text4</text:p>
Run Code Online (Sandbox Code Playgroud)
您可以在text:span标记与text2和text3之间看到空格.当我调用XmlDocument.Load方法时,我有这样的图片:
<text:p>text1 <text:span>text2</text:span><text:span>text3</text:span>text4</text:p>
Run Code Online (Sandbox Code Playgroud)
白色空间被移除了,但我在这个地方需要这个空间.设置属性"PreserveWhitespace = true"没有帮助
我使用XmlDocument来解析xml文件,但似乎XmlDocument始终将xml注释作为xml节点读取:
我的C#代码
XmlDocument xml = new XmlDocument();
xml.Load(filename);
foreach (XmlNode node in xml.FirstChild.ChildNodes) {
}
Run Code Online (Sandbox Code Playgroud)
Xml文件
<project>
<!-- comments-->
<application name="app1">
<property name="ip" value="10.18.98.100"/>
</application>
</project>
Run Code Online (Sandbox Code Playgroud)
.NET不应该跳过XML注释吗?
我想弄清楚如何找到 .a 中字符串的所有匹配项XmlDocument。
XmlNodeList results
= document.SelectNodes("Products/Product/fn:matches(.,'" + SearchWord + "')");
Run Code Online (Sandbox Code Playgroud)
我正在尝试比较innerText产品。
虽然上面的例子不起作用,但我想我使用 XPath 函数的方式是非常错误的。
我有一些遗留的XML文档作为blob存储在数据库中,它们不是格式良好的XML.我正在从SQL数据库中读取它们,最终,当我使用C#.NET时,希望将它们实例化为XMLDocument.
当我尝试这样做时,我显然得到了一个XMLException.看过XML文档之后,由于特定XML节点中未声明的命名空间,它们都失败了.
我并不关心任何具有此前缀的XML节点,因此我可以忽略它们或将它们丢弃.所以基本上,在我将字符串作为XMLDocument加载之前,我想删除字符串中的前缀,以便这样做
<tem:GetRouteID>
<tem:PostCode>postcode</tem:PostCode>
<tem:Type>ItemType</tem:Type>
</tem:GetRouteID>
Run Code Online (Sandbox Code Playgroud)
变
<GetRouteID>
<PostCode>postcode</PostCode>
<Type>ItemType</Type>
</GetRouteID>
Run Code Online (Sandbox Code Playgroud)
还有这个
<wsse:Security soapenv:actor="">
<wsse:BinarySecurityToken>token</wsse:BinarySecurityToken>
</wsse:Security>
Run Code Online (Sandbox Code Playgroud)
成为这个:
<Security soapenv:actor="">
<BinarySecurityToken>token</BinarySecurityToken>
</Security>
Run Code Online (Sandbox Code Playgroud)
我有一个解决方案,这样做:
<appSettings>
<add key="STRIP_NAMESPACES" value="wsse;tem" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
if (STRIP_NAMESPACES != null)
{
string[] namespaces = Regex.Split(STRIP_NAMESPACES, ";");
foreach (string ns in namespaces)
{
str2 = str2.Replace("<" + ns + ":", "<"); // Replace opening tag
str2 = str2.Replace("</" + ns + ":", "</"); // Replace closing tag
}
}
Run Code Online (Sandbox Code Playgroud)
但理想情况下,我想要一个通用的方法,所以我不必无休止地配置我想删除的命名空间.
我怎样才能在C#.NET中实现这一点.我假设一个正则表达式是去这里的方式?
更新1
Ria的正则表达式适用于上述要求.但是,我如何更改正则表达式以更改此值
<wsse:Security soapenv:actor="">
<BinarySecurityToken>authtoken</BinarySecurityToken>
</Security>
Run Code Online (Sandbox Code Playgroud)
这个? …
我XmlDocument()用于解析文件,如*.opf*,用于我的EpubReader应用程序.
<item id="W01MB154" href="01MB154.html" media-type="application/xhtml+xml" />
<item id="W000Title" href="000Title.html" media-type="application/xhtml+xml" />
Run Code Online (Sandbox Code Playgroud)
同
<itemref idref="W000Title" />
<itemref idref="W01MB154" />
Run Code Online (Sandbox Code Playgroud)
这些值在同一个文件中..
在这里我知道了item的标签中id的值,之后我想知道href元素的值.
我需要做的是比较值IDREF在elemnt 的itemref用的元素值标签ID的标签项目.在这里我知道id值是W01MB154.
简单地说,我想知道id的下一个值是href元素,使用XmlDocument(),
如果有人意识到这种情况,请帮帮我.
谢谢,
<?xml version="1.0" encoding="utf-8" ?>
<testcase>
<date>4/12/13</date>
<name>Mrinal</name>
<subject>xmlTest</subject>
</testcase>
Run Code Online (Sandbox Code Playgroud)
我试图使用c#读取上面的xml,但是我在try catch块中得到null异常可以任何正文建议所需的更改.
static void Main(string[] args)
{
XmlDocument xd = new XmlDocument();
xd.Load("C:/Users/mkumar/Documents/testcase.xml");
XmlNodeList nodelist = xd.SelectNodes("/testcase"); // get all <testcase> nodes
foreach (XmlNode node in nodelist) // for each <testcase> node
{
CommonLib.TestCase tc = new CommonLib.TestCase();
try
{
tc.name = node.Attributes.GetNamedItem("date").Value;
tc.date = node.Attributes.GetNamedItem("name").Value;
tc.sub = node.Attributes.GetNamedItem("subject").Value;
}
catch (Exception e)
{
MessageBox.Show("Error in reading XML", "xmlError", MessageBoxButtons.OK);
}
Run Code Online (Sandbox Code Playgroud)
........ .....
使用下面的代码,我可以在XmlDocument xWorkload中加载一个Xml文件.
XmlDocument xWorkload = new XmlDocument();
private void button1_Click(object sender, RoutedEventArgs e)
{
var outputxml = new StringBuilder(string.Empty);
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "demo"; // Default file name
dlg.DefaultExt = ".xml"; // Default file extension
dlg.Filter = "Xml documents (.xml)|*.xml"; // Filter files by extension
var result = dlg.ShowDialog(); //Opens the dialog box
if (result == true)
{
xWorkload.Load(dlg.FileName);
string Path = dlg.FileName.Replace(dlg.SafeFileName, "");
}
}
Run Code Online (Sandbox Code Playgroud)
假设,文件夹中有多个Xml文件,我想在xWorkload中加载所有Xml文件,并将这些xml文件存储在一个字符串中我该怎么做?这可以在wpf中使用XmlDocument来完成(不是LINQ).PLZ建议
我有一个像这样的 XML 文件:
<Something>....</Something>
<Another>....</Another>
<Other>...</Other>
Run Code Online (Sandbox Code Playgroud)
这个 XML 文件没有根元素(我知道这是一种错误的 XML 格式)。
我需要在这个 XML 中创建或替换(如果它已经存在)一个节点,但我不能使用XDocument或者XmlDocument因为他们需要一个根元素才能工作,我不能向这个 XML 添加一个根元素,因为我可以'不要更改应用程序(Windows 窗体应用程序)中的更多代码。
我有哪些选择来执行此操作?
编辑 1:使用@chridam 的示例,我有这个方法,但它替换了整个 XML。我需要改变什么?
public void ReEscribirNodoXML(string pathXml, string nodoName, string nodeContent)
{
if (File.Exists(pathXml))
{
XmlValidatingReader vr = new XmlValidatingReader(pathXml, XmlNodeType.Element, null);
while (vr.Read())
{
Debug.WriteLine("NodeType: {0} NodeName: {1}", vr.NodeType, vr.Name);
}
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineOnAttributes = true;
var writer = XmlWriter.Create(pathXml, settings);
writer.WriteStartElement(nodoName);
writer.WriteRaw(nodeContent);
writer.WriteEndElement();
writer.Flush();
}
else …Run Code Online (Sandbox Code Playgroud) 我有一些代码来替换XML文档的根节点名称,同时保留其名称空间.
XmlDocument doc = new XmlDocument();
Stream inStream = inmsg.BodyPart.GetOriginalDataStream();
doc.Load(inStream);
XmlNode root = doc.DocumentElement;
XmlNode replacement = doc.SelectSingleNode("/*/*[1]");
XmlNode newRoot = doc.CreateElement(replacement.Name);
XmlAttribute xmlns = (XmlAttribute)root.Attributes["xmlns"].Clone();
newRoot.Attributes.Append(xmlns);
newRoot.InnerXml = root.InnerXml; //the problem is here!
doc.ReplaceChild(newRoot, root);
Run Code Online (Sandbox Code Playgroud)
使用如下所示的文档:
<OLD_ROOT xmlns="http://my.xml.namespace">
<NEW_ROOT>
Run Code Online (Sandbox Code Playgroud)
它导致:
<NEW_ROOT xmlns="http://my.xml.namespace">
<NEW_ROOT xmlns="http://my.xml.namespace">
Run Code Online (Sandbox Code Playgroud)
第二个xmlns是因为该InnerXml属性显然将其设置在其内容的第一个节点上!我可以做些什么来规避这一点,而不必在事后删除它?
尝试使用以下代码
XmlNode first_node = doc.SelectSingleNode("/*/*[1]");
XmlAttribute excess_xmlns = first_node.Attributes["xmlns"];
first_node.Attributes.Remove(excess_xmlns);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为xmlns显然不存在该节点上的属性!
这是来自MSDN的示例xml
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
</book>
</bookstore>
Run Code Online (Sandbox Code Playgroud)
当我使用以下代码选择所有书籍节点时,这些节点将具有哪个顺序?
XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");
var nodeList =doc.SelectNodes("bookstore/book");
Run Code Online (Sandbox Code Playgroud)
nodelist中项目的顺序是否与xml中的顺序相同?这个订单有保证吗?
xmldocument ×10
c# ×9
xml ×7
.net ×3
xpath ×2
asp.net ×1
linq-to-xml ×1
parsing ×1
regex ×1
wpf ×1
xml-comments ×1